Jump to content

Get Player Ped from Client Event


DarkStalker30

Recommended Posts

So, I have a client-side event:

addEventHandler("onClientKey", root, function (button,press) 
    if button == "j" then
        triggerServerEvent("jetpackClicked", root)
    end
end)

And I have server-side code:

addEvent("jetpackClicked", true)
addEventHandler('jetpackClicked', getRootElement(), function ( root )
	setPedWearingJetpack ( root, not isPedWearingJetpack(root) )
end)

But it's not working because "root" is not a ped. How can I fix it?

Link to comment

Try this:

 

Client-side:

addEventHandler("onClientKey", root, function (button,press) 
    if button == "j" then
        triggerServerEvent("jetpackClicked", localPlayer)
    end
end)

 

Server-side:

addEvent("jetpackClicked", true)
addEventHandler('jetpackClicked', getRootElement(), function ( )
	setPedWearingJetpack ( source, not isPedWearingJetpack( source ) )
end)

 

Link to comment
8 minutes ago, Sousateew said:

Try this:

 

Client-side:

addEventHandler("onClientKey", root, function (button,press) 
    if button == "j" then
        triggerServerEvent("jetpackClicked", localPlayer)
    end
end)

 

Server-side:

addEvent("jetpackClicked", true)
addEventHandler('jetpackClicked', getRootElement(), function ( )
	setPedWearingJetpack ( source, not isPedWearingJetpack( source ) )
end)

 

It's really working, very thx! :)

9 minutes ago, Sousateew said:

Try this:

 

Client-side:

addEventHandler("onClientKey", root, function (button,press) 
    if button == "j" then
        triggerServerEvent("jetpackClicked", localPlayer)
    end
end)

 

Server-side:

addEvent("jetpackClicked", true)
addEventHandler('jetpackClicked', getRootElement(), function ( )
	setPedWearingJetpack ( source, not isPedWearingJetpack( source ) )
end)

 

But sometimes it's lagging and jetpack is on/off many times in a second. Can I fix it somehow?

Link to comment
2 minutes ago, DarkStalker30 said:

It's really working, very thx! :)

But sometimes it's lagging and jetpack is on/off many times in a second. Can I fix it somehow?

Oh, sorry. I did not realize it.

 

addEventHandler("onClientKey", root, function (button,press) 
    if button == "j" and not press then
        triggerServerEvent("jetpackClicked", localPlayer)
    end
end)

Or also, you can use bindKey

bindKey("j", "down", function()
	triggerServerEvent("jetpackClicked", localPlayer)
end)

 

Link to comment
1 minute ago, Sousateew said:

Oh, sorry. I did not realize it.

 

addEventHandler("onClientKey", root, function (button,press) 
    if button == "j" and not press then
        triggerServerEvent("jetpackClicked", localPlayer)
    end
end)

Or also, you can use bindKey

bindKey("j", "down", function()
	triggerServerEvent("jetpackClicked", localPlayer)
end)

 

I should have understood it by myself ? Very thx!

  • Like 1
Link to comment
addEventHandler("onClientKey", root, function (button,press) 
    if button == "j" then
        triggerServerEvent("jetpackClicked", localPlayer)
    end
end)
addEvent("jetpackClicked", true)
addEventHandler('jetpackClicked', getRootElement(), function ( )
	setPedWearingJetpack ( source, not isPedWearingJetpack( source ) )
end)
Edited by Sr.black
Link to comment
  • 5 months later...

The issue in your code is that you are passing the "root" element to the server-side function as the argument and trying to use it as a player (ped). The "root" element is typically used on the client-side to refer to the root element of the resource, and it does not represent a player or ped on the server-side.

To fix this, you need to pass the correct player (ped) element from the client-side to the server-side. You can do this by using the "source" keyword on the server-side, which represents the player who triggered the event. Here's the modified code:

Client-side code:

 
lua
addEventHandler("onClientKey", root, function (button, press) 
    if button == "j" then
        triggerServerEvent("jetpackClicked", localPlayer) -- Pass localPlayer as an argument
    end
end)

Server-side code:

 
addEvent("jetpackClicked", true)
addEventHandler('jetpackClicked', root, function ()
    local player = source -- Use the "source" keyword to get the player element
    if player and isElement(player) and getElementType(player) == "player" then
        setPedWearingJetpack(player, not isPedWearingJetpack(player))
    end
end)

Now, when the client triggers the "jetpackClicked" event, it will pass the localPlayer (the player who pressed the "j" key) as the argument to the server-side function. The server-side function will then use the "source" keyword to get the correct player element and toggle their jetpack accordingly.

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...