DarkStalker30 Posted February 15, 2023 Share Posted February 15, 2023 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
sousateew Posted February 15, 2023 Share Posted February 15, 2023 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
DarkStalker30 Posted February 15, 2023 Author Share Posted February 15, 2023 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
sousateew Posted February 15, 2023 Share Posted February 15, 2023 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
DarkStalker30 Posted February 15, 2023 Author Share Posted February 15, 2023 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! 1 Link to comment
Sr.black Posted February 15, 2023 Share Posted February 15, 2023 (edited) 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 February 15, 2023 by Sr.black Link to comment
Rilot Posted February 16, 2023 Share Posted February 16, 2023 Use 'client' instead of 'source' Link to comment
ExMohmD Posted August 4, 2023 Share Posted August 4, 2023 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
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now