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

How do i make it so that the timer from the line 14 goes off only after a player has jumped?

Also, how do i make it so it cannot be triggered again while it's active? (btw, this is a local script from StarterGui)

Cooldown = 10

repeat wait() until script.Parent.Parent.Character
Character = script.Parent.Parent.Character

local Human = Character:WaitForChild("Humanoid")
local LastJump = time()

Human.Changed:connect(function(Prop)
	if Prop and Prop == "Jump" and Human.Jump then
		local CurrentTime = time()
		if LastJump + Cooldown > CurrentTime then
			Human.Jump = false
			for i = 10, 0, -1 do
               script.Parent.JumpCD.TextLabel.Text = "Jump Cooldown: " ..i
                wait(1)
                end
		else
			LastJump = CurrentTime
		end
	end
end)

1 answer

✓ Accepted answer

Instead of using a Changed event on the Humanoid, use the Jumping event. We can also disable the Humanoid from jumping internally using SetStateEnabled.

This code snippet below should work, but if it doesn't please let me know. I've added a few conditions: do not start cooldown if the player cannot jump in the first place, and to stop changing the TextLabel when the player died. I disable the humanoid from jumping until it requests the jump.

local PlayerService = game:GetService("Players")
local Heartbeat = game:GetService("RunService").Heartbeat

--Player objects.
local Player = PlayerService.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local Humanoid = Character:WaitForChild("Humanoid")

--Configurations.
local Cooldown = 3
local Debounce = false

--Other objects.
local TextLabel = script.Parent

local function JumpWithCooldown()
    --Check if the player is not on air.
    if not Debounce then
        Debounce = true
        Humanoid.Jump = true
		Heartbeat:Wait()
        --Disables the humanoid from jumping.
        Humanoid:SetStateEnabled(Enum.HumanoidStateType.Jumping, false)

        for i = Cooldown, 0, -1 do
            --We want this to stop printing when the humanoid dies.
            if Humanoid:GetState() == Enum.HumanoidStateType.Dead then
                TextLabel.Text = "You died. " .. i
                break
            end

            TextLabel.Text = "Jump cooldown: " .. i
            wait(1)
        end

        --Reenable jumping.
        Debounce = false
		Heartbeat:Wait()
        Humanoid:SetStateEnabled(Enum.HumanoidStateType.Jumping, true)
    end
end

Humanoid.Jumping:Connect(JumpWithCooldown)

5 comments

0

Unfortunately it does not work all the time, i have to jump a couple of times for it to activate, movement might also be affecting it but i'm not sure (this leads to getting 'free' jumps). There are no errors in the output, was this not supposed to be a Local Script located in StarterGui?

0

I fixed it. Can you try it now? I disable the humanoid from jumping until it requests to jump, enable it, set jump to true, then I disable it again.

0

It does not work at all now (jumping has been completely disabled), i have managed to jump only once (i'm not sure how though) but the cooldown was not going off at all.

0

I removed JumpRequest and changed it to Humanoid.Jumping. Can you try again?

0

It works flawlessly now, thank you very much.

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