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

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

Asked by 4 years ago
Edited 4 years ago

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

Log in to vote
0
Answered by
Rheines 661 Moderation Voter
4 years ago
Edited 4 years ago

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 TextLabelwhen 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)
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? YoBoi1337 25 — 4y
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. Rheines 661 — 4y
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. YoBoi1337 25 — 4y
0
I removed JumpRequest and changed it to Humanoid.Jumping. Can you try again? Rheines 661 — 4y
0
It works flawlessly now, thank you very much. YoBoi1337 25 — 4y
Ad

Answer this question