I want a RemoteEvent to fire when the player presses jump. My original script worked for PC (mobile and console players could still jump, but event did not fire).
Here is the original script:
--// in StarterPlayer.StarterPlayerScripts
local plr = game.Players.LocalPlayer
local Remote = workspace.Events.AddPoints
local Debounce = true
local Char = plr.Character or plr.CharacterAdded:Wait()
local Key = 'MouseButton1Down'
local mouse = plr:GetMouse()
mouse.KeyDown:Connect(function(key)
if key:byte() == 32 then
if Debounce and Char and Char.Humanoid.Jump == false then
Debounce = false
Remote:FireServer()
end
end
wait(2)
Debounce = true
end)
I then got help on DevForum *the script below works now on all platforms) and had it changed to:
--credit: corotines
-- StarterPlayer.StarterCharacterScripts
local Workspace = game:GetService("Workspace")
local Humanoid = script.Parent:WaitForChild("Humanoid")
local Remote = Workspace:WaitForChild("Events"):WaitForChild("AddPoints")
Humanoid.Jumping:Connect(function(active)
if active then
Remote:FireServer()
end
end)
However, now the players can hold down the jump button and it keeps firing the event instead of having to click for each time the event is fired. I’ve added a debounce, but that doesn’t help either. Also tried to add: if active and humanoid.Jump == false then
but the problem remains.
local userInputService = game:GetService("UserInputService")
function jumpRequest()
if Debounce then
Remote:FireServer()
Debounce = false
end
wait(2)
Debounce = true
end
userInputService.JumpRequest:Connect(jumpRequest)
I tried implementing the if Humanoid.FloorMaterial == Enum.Material.Air then Midair = true but I got the same issue. Am I doing this wrong?
local userInputService = game:GetService("UserInputService")
local Midair = false
function jumpRequest()
if Humanoid.FloorMaterial == Enum.Material.Air then Midair = true
end
if Midair == false then
Remote:FireServer()
end
wait(2)
end
I got an error message for Enum, so I changed it to Enum.HumanoidStateType.Landed (now there is no error). However, I have the same issue. Jumpbutton/Spacebar can be held down and the event keeps firing. Below is my code:
Humanoid.StateChanged:Connect(function(oldstate, newstate)
if newstate == Enum.HumanoidStateType.Landed then
Debounce = true
Remote:FireServer()
Debounce = false
end
wait(2)
Debounce = true
end)
The idea is not to use a wait(2). You want to set debounce to true when you land (land state) which only fires once due to the StateChanged event. When you jump, you can use the jumpRequest you used earlier, but again without a wait(2).
Here’s what I mean if I am explaining this poorly in words,
local userInputService = game:GetService("UserInputService")
function jumpRequest()
if Debounce then
Remote:FireServer()
Debounce = false
end
end)
userInputService.JumpRequest:Connect(jumpRequest)
-- Now you also need to check for when the player lands,
Humanoid.StateChanged:Connect(function(oldstate, newstate)
if newstate == Enum.HumanoidStateType.Landed then
Debounce = true
end
end)
I hope that clarifies what I meant. Sorry if this was a little fuzzy in the initial reply.
Thanks for taking the time. I removed the ) after end at the end of the jumprequest(). The code works the same as the others. If I press the jump button and hold it down, the event keeps firing without the player having to press the button again.
The FireServer gets called repeatedly if I hold down the spacebar (well, and so does the “jumping event”). The goal is that the player has to press the jump button (depends on the platform what button that is) each time for the FireServer to get triggered and ideally the player has to land again before the FireEvent triggers when the button is pressed (to do this, your code with the Humanoid.StateChanged would work embedded in some other code that eludes me at the moment).
Ahh, you mean you need an input release so you can’t hold the dedicated button? That means you need a second state that determines if the key was released. A second debounce.
Also, are you saying that the FireServer is only occurring in intervals then, not the entire time the button is held down? As in, the debounce is working properly?
I tested with print statements and a higher jump power to double-check as it was hard to see if the jumping is only a short distance. Happy to say the debounce is working correctly : ) as in, the FireServer only gets triggered after the player lands and gets in the air again.
This means that the button release is still needed. In my original code, this worked all fine for PC, but not for mobile/tablet/console and I haven’t found a way to make that happen yet.
This is the script that worked only on PC but not the others (for the others, no FireServer got triggered at all but they could still jump):
local plr = game.Players.LocalPlayer
local Remote = workspace.Events.AddPoints
local Debounce = true
local Char = plr.Character or plr.CharacterAdded:Wait()
local Key = 'MouseButton1Down'
local mouse = plr:GetMouse()
mouse.KeyDown:Connect(function(key)
if key:byte() == 32 then
if Debounce and Char and Char.Humanoid.Jump == false then
Debounce = false
Remote:FireServer()
end
end
wait(2)
Debounce = true
end)
You can’t use KeyDown for anyone who doesn’t have keyboard. Instead you may have to use UserInputService and get input.Released(input) or something (I’m not sure what the API is exactly). I believe someone else had a thread today where they used this so I know it is supported for mobile and console jumps. You may need to read a bit of the wiki for the exact API.
Also, glad that the debounce is functioning. Hopefully by integrating this the script will function fully. Good luck.
I think I am just saying what everyone else is saying. But I wrote all this last night before I got disconnected so I might as well post it.
Maybe something like this could work?
local Workspace = game:GetService("Workspace")
local Humanoid = script.Parent:WaitForChild("Humanoid")
local Remote = Workspace:WaitForChild("Events"):WaitForChild("AddPoints")
JumpingCheck = false
Humanoid.Jumping:Connect(function(active)
if active and JumpingCheck = false then
Remote:FireServer()
JumpingCheck = true
end
end)
humanoid.Climbing:Connect(function(speed)
JumpingCheck = false
end)
humanoid.FallingDown:Connect(function(isActive)
JumpingCheck = false
end)
humanoid.GettingUp:Connect(function(isActive)
JumpingCheck = false
end)
humanoid.Jumping:Connect(function(isActive)
JumpingCheck = false
end)
humanoid.PlatformStanding:Connect(function(isActive)
JumpingCheck = false
end)
humanoid.Ragdoll:Connect(function(isActive)
JumpingCheck = false
end)
humanoid.Running:Connect(function(speed)
JumpingCheck = false
end)
humanoid.Strafing:Connect(function(isActive)
JumpingCheck = false
end)
humanoid.Swimming:Connect(function(speed)
JumpingCheck = false
end)
Not sure but I would have something that makes sure there is a break in the jumping, and then have that turn the jumpingCheck to false.
If it was just PC, I would have it if the jumping key is not pressed, then set it to false. That way it won’t keep going.
However, you seem to have it so that holding the jump will keep jumping no matter what, therefore triggering the event. Which if it’s still jumping onscreen, it should still get points.
So you would have to control the jumping thing so that as soon as the jump is started, the input is forced to stop and people then must release it to then press it again to initiate another jump. It’s more about the “If Key pressed, then jump and stop the key pressing until the key pressed is released.”
That’s in whatever code there is before this section. And if you fix the problem back there, then you don’t have to worry about the JumpCheck stuff I have here. The check will be back in the input code instead of the jump code. Same principle applies though. The action of releasing will set JumpCheck to false. Then the next press will work.
It’s like a debounce but rather than the time controlling when it can jump again, it’s the release of the key that controls when they can jump again.
I’m pretty sure there is something for the other platforms that works like the initial code I had with the mouse.KeyDown, and I have looked at UserInputService. I’m just not figuring it out at the moment (I’m new to scripting so I’ve got a ways to go). Once I find out how it’s done, I’ll post it here.