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

local script in player's gui won't track if jump key is pressed?

Asked by 3 years ago

heyyyy i am trying to make a jump stamina feature script is parented under a frame in a screen gui in the starter gui

local players = game:GetService("Players")
local player = players.LocalPlayer
local UIS = game:GetService("UserInputService")

local jumps_left_value = script.Parent.jumps_left.Value
--local transparency_module = script.Parent.transparency_script
local size_script = script.Parent.size_script

function jump_pressed()
    jumps_left_value = jumps_left_value - 1
    print(jumps_left_value .. " jump(s) left")
end

if UIS:IsKeyDown(Enum.KeyCode.Space) then
    jump_pressed()
    print("player jumped")
end

there is no error but i know i am not getting a player jumped in output

1 answer

Log in to vote
0
Answered by
Rare_tendo 3000 Moderation Voter Community Moderator
3 years ago
Edited 3 years ago

A few problems in your code:

  • Variables do not reference properties: The variable jumps_left_value only stores the current value and changes made to the variable will not be reflected to the jumps_left IntValue/NumberValue

  • The if statement will only run once: Because the if statement is not wrapped in a loop / event that can fire many times, the if statement is only ran once and if the SpaceBar isn't held down, the if statement's scope will not be entered so the code inside will not be ran and it is never checked again

Conveniently, UserInputService provides an event called JumpRequest which fires when the client attempts to jump. Here, you can enable/disable jumps:

local jumps_left = script.Parent.jumps_left
local player = game.Players.LocalPlayer
local humaoid = player.Character:WaitForChild("Humanoid")

UIS.JumpRequest:Connect(function()
    jumps_left.Value -= 1 -- "-=" is a compound assignment operator that is the same as "jumps_left.Value = jumps_left.Value - 1"
    -- humanoid:SetStateEnabled(Enum.HumanoidStateType.Jumping, false/true) : this line can enable and disable jumping. Passing "true" will enable, passing "false" will disable
end)
Ad

Answer this question