I need to change an IntValue whenever a player jumps. The code below subtracts the value by two anytime I jump, instead of one. I want the value to only subtract by one. How can I do this properly?
local plr = game.Players.LocalPlayer plr.Character.Humanoid.Jumping:Connect(function() thirstValue.Value = thirstValue.Value - 1 end)
The problem here is that the Jumping
event fires when the character starts jumping and when the character has landed. In order to fix this, check whether or not the active
parameter passed through the event is true
.
local plr = game.Players.LocalPlayer plr.Character.Humanoid.Jumping:Connect(function(active) if active then thirstValue.Value = thirstValue.Value - 1 end end)
Alternatively, you can use the StateChanged
event. Not only does it fix your problem, it's also way more flexible.
local plr = game.Players.LocalPlayer plr.Character.Humanoid.StateChanged:Connect(function(oldState , newState) if newState == Enum.HumanoidStateType.Jumping then thirstValue.Value = thirstValue.Value - 1 end end)
The function first checks whether or not the newState
parameter is the Jumping
state. Then, it decrements thirstValue
by one.
This Is How I Would Do It
local plr = game.Players.LocalPlayer local UIS = game:GetService("UserInputService") UIS.InputBegan:Connect(function(key) if key.KeyCode == Enum.KeyCode.Space then -- Your Code Here end end)
Since You Need To Press Space To Jump Then You Could See If They Did To Do Your Script