Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How to reference a player's jump?

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)

2 answers

✓ Accepted answer

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.

1 comment

0

Perfect. Thanks for the explanation.

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

This discussion is preserved as it was. New answers, comments, and votes are closed.