Jump to content

Run server event for each player separately


Recommended Posts

Hello everyone, again I'm asking help, hope u can do it :)

 

I'm creating system of interiors and what I want: two (or more) players using the marker to enter, and server does calculations for all of them throw one event. Here code I have:

Client:

addEventHandler("onClientResourceStart", resourceRoot, function()
    bindKey("lalt", "down", function(key, state)
        local isReadyForUseMarker = getElementData(localPlayer, "player.activeMarker")
        if isElement(isReadyForUseMarker) and (getElementType(isReadyForUseMarker) == "marker" or getElementType(isReadyForUseMarker) == "pickup") then
            triggerServerEvent("teleportByMarker", localPlayer)
        end
    end)
end)

Server for teleport:

addEvent("teleportByMarker", true)
addEventHandler("teleportByMarker", root, function()
    if not client then
        return
    end
    local marker = getElementData(client, "player.activeMarker")
    if isElement(marker) and (getElementType(marker) == "marker" or getElementType(marker) == "pickup") then
        local x = getElementData(marker, "teleport.destinationX")
        local y = getElementData(marker, "teleport.destinationY")
        local z = getElementData(marker, "teleport.destinationZ")
        local world = getElementData(marker, "teleport.destinationWorld")
        local rotation = getElementData(marker, "teleport.destinationRotation")
        local dimension = getElementData(marker, "teleport.destinationDimension")
        if x and y and z and world and dimension and rotation then
            player = client
            setElementData(player, "player.activeMarker", false)
            fadeCamera(player, false)            
            toggleAllControls(player, false)
            setElementFrozen(player, true)
            setTimer(function ()
                triggerClientEvent(player, "toggleServerHud", player)
            end, 900, 1)
            setTimer(function ()                
                setElementInterior(player, world, x, y, z)
                setElementRotation(player, 0, 0, rotation)
                setElementDimension(player, dimension)                
            end, 1500, 1)
            setTimer(function ()
                setCameraTarget(player)
                fadeCamera(player, true)
                setTimer(function ()
                    triggerClientEvent(player, "toggleServerHud", player)
                    setElementFrozen(player, false)
                    toggleAllControls(player, true) 
                end, 900, 1)                               
            end, 1500, 1)
        else
            outputDebugString('Not full list of arguments for TP!')
        end
    else
        outputDebugString('Triggered event for TP, but player has no data about marker.')
    end
end)

It works great for one player, but if many players try to enter at the same time, it breaks and do the code only for one player.

How can I parallel this code for all players, which want to enter?

Link to comment
  • Moderators
23 minutes ago, DarkStalker30 said:
        if x and y and z and world and dimension and rotation then
            player = client
            setElementData(player, "player.activeMarker", false)

 

        if x and y and z and world and dimension and rotation then
            local player = client -- < HERE
            setElementData(player, "player.activeMarker", false)

Make `player` a local variable. Else all players will share the variable `player` for the (delayed) timer functions.

 

You can also pass the predefined `client` variable like this:

setTimer(function (player)

end, 1500, 1, client)

 

  • Thanks 1
Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...