local function OnStateChanged(old: Enum.HumanoidStateType, new: Enum.HumanoidStateType)
print(old, new) -- Running -> Freefalling and vice versa
if new ~= Enum.HumanoidStateType.Freefall then return end
jumpingStamina.Value -= 20
regeneratingJumpingStamina.Value = false
end
i run this code snippet on the server, and the issue with this is that it runs when the player falls off a ledge, this is expected
but i only want the code to run when the player actually jumps, but checking for Jumping state on the server is extremely inconsistent and i really don’t want to fire a remote event every time the player jumps
Using the client would be the smoothest experience, but you could try relying on the Humanoid’s Jump property via the GetPropertyChangedSignal function:
Snippet
local cooldown
humanoid:GetPropertyChangedSignal('Jump'):Connect(function()
if not cooldown and humanoid.Jump then
cooldown = true
task.spawn(function()
task.wait(.25)
cooldown = nil
end)
print(player.Name .. ' jumped!')
-- do stuff
end
end)
Example
local players = game:GetService('Players')
players.PlayerAdded:Connect(function(player)
player.CharacterAdded:Connect(function(character)
local humanoid,died,destroying = character:WaitForChild('Humanoid')
local cooldown
local jumped = humanoid:GetPropertyChangedSignal('Jump'):Connect(function()
if not cooldown and humanoid.Jump then
cooldown = true
task.spawn(function()
task.wait(.25)
cooldown = nil
end)
print(player.Name .. ' jumped!')
-- do stuff
end
end)
local function cleanUp()
died:Disconnect()
destroying:Disconnect()
jumped:Disconnect()
humanoid,died,destroying,jumped = nil
end
died = humanoid.Died:Once(cleanUp)
destroying = character.Destroying:Once(cleanUp)
end)
end)
The cooldown seems to be needed as it triggers multiple time when the user is on the ground and pressing jump.
Your code snippet looks fine, the only thing is that i would check if new state is Jumping.
How is it extremely inconsistent? what problems did it give you? have you tried it even?
The alternative methods would be if you have a custom movement system. However, i do not think its necessary as the state is enough. Again, how is it inconsistent? just asking.
If you are talking about the player being able to spam, you can just set a variable IsJumping where you set it to true when the humanoud jumps, and sets it to false if the players starts, for example, Swimming or Landing. Or running, as long as its not freefall and Jumping.