local tryToJump = false
UIS.JumpRequest:Connect(function()
tryToJump = true
end)
print(tryToJump)
this prints false, why? and how to make it actually work?
local tryToJump = false
UIS.JumpRequest:Connect(function()
tryToJump = true
end)
print(tryToJump)
this prints false, why? and how to make it actually work?
because youre printing the trytojump variable instantly when the code starts running try moving the print to inside of the jumprequest event
Your code actually works the way it is supposed to. The UserInputService.JumpRequest event creates a new thread Every time you use :Connect(function() ... end), you create a new thread.
The way your code gets executed is as follows:
RBXScriptConnection on the UserInputService.JumpRequest event is made. This event has not yet been fired, so we move on. Thus, we do not set the tryToJump variable to true.tryToJump variable, which is still false.Some time later in the game, the player decides to jump:
4. We set the tryToJump variable to true.
To see the previously described behavior in action, you can use this code snippet:
local UserInputService = game:GetService("UserInputService")
local playerJumped: boolean = false
local _connection: RBXScriptConnection = UserInputService.JumpRequest:Once(function()
print("The player has jumped!")
print("Setting playerJumped to true!")
playerJumped = true
end)
while true do
task.wait(1)
print(string.format("PlayerJumped: %s", tostring(playerJumped)))
end
The preceding code snippet listens to the UserInputService.JumpRequest event, when this is fired, it sets the playerJumped variable to true.
At the bottom of the code snippet, we also have a while loop, this prints the value of the playerJumped variable every second. If you run this script, you will notice that before you jumped, the playerJumped variable will remain false, and once the player jumped, it changes to true.
I hope this is a clear explanation ![]()
If you want to know more, please look into the RBXScriptConnection.