Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How to reference a player's jump?

Asked by
Hypgnosis 186
6 years ago

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

Log in to vote
1
Answered by
RayCurse 1518 Moderation Voter
6 years ago

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.

0
Perfect. Thanks for the explanation. Hypgnosis 186 — 6y
Ad
Log in to vote
0
Answered by 6 years ago

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

Answer this question