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

How to detect if a humanoid is climbing for 3 seconds continuously?

Asked by
Aznarog 54
5 years ago
Edited 5 years ago

Pretty much i want to detect if a humanoid is climbing (stationary climb or moving climb) for 3 seconds continuously, and if so push him off where he is climbing.

I tried this code but it doesn't work because it executes the function upon the humanoid climbing and does not take into consideration if the player has been climbing for 3 seconds continuously - I want to take this into consideration.

local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:wait()
local rootpart = character.HumanoidRootPart
character.Humanoid.Climbing:Connect(function()
    wait(3)         
    rootpart.Velocity = rootpart.CFrame.lookVector*-50
end)

my friend wrote this code for me and i also do not understand why he added the " or player.CharacterAdded:wait() ".

local character = player.Character or player.CharacterAdded:wait()

thank you!

1 answer

Log in to vote
1
Answered by
ozzyDrive 670 Moderation Voter
5 years ago
Edited 5 years ago

In your code snippet you aren't checking if the player is still climbing after the 3 seconds have passed. You can get the humanoid's current state with the GetState method. Just checking if the state is still "Climbing" may not be enough for you as the player might've started climbing again during that 3 seconds. To avoid this, you can listen to state changes and cancel the procedure if the state changed before the 3 seconds passed.

local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")

local function flingCharacter(character, force)
    force = force or -50
    character.PrimaryPart.Velocity = character.PrimaryPart.CFrame.lookVector * force
end

humanoid.Climbing:Connect(function()
    --  Listen for changes in the humanoid's state
    local stateChangedConnection 
    stateChangedConnection = humanoid.StateChanged:Connect(function(_, newState)
        --  If the player jumped, then drop them from the ladder
        if newState == Enum.HumanoidStateType.Jumping then
            flingCharacter(character)
        end
        stateChangedConnection:Disconnect()
    end)

    wait(3)

    --  If the state hasn't changed, we can assume they were climbing the whole time
    if stateChangedConnection.connected then
        stateChangedConnection:Disconnect()
        flingCharacter(character)
    end
end)

local character = player.Character or player.CharacterAdded:Wait() This line of code is there in case the character existed before the script was executed. It assigns the current character of the player to the "character" variable and if it's not found, it waits until the character is added using the Wait method of RBXScriptSignal. The CharacterAdded event's only parameter is the new character, which is then assigned to the "character" variable as the Wait method returns it.

0
Thank you so much man this is perfect, works like a charm and it makes sense!!! (for most part, I have to learn more :P) I am new to programming and all and so I truly appreciate the work and effort you have provided!!!! Thank you again :) Aznarog 54 — 5y
Ad

Answer this question