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

How to detect if player is standing still using an if statement?

Asked by 3 years ago

I am trying to make a sprinting script both for mobile and pc, But the thing is that once your performing an animation like walking and click shift the animation will still play even if your standing still so im trying to make it so that you have to stand still to run using an if statement here is the code


local Player = game.Players.LocalPlayer local Character = Player.Character local RunningSpeed = 22 local WalkingSpeed = 16 UIS.InputBegan:Connect(function(input) if input.KeyCode == Enum.KeyCode.LeftShift then if Character.Humanoid.Jump == false then if Character.Humanoid.MoveDirection == ("0, 0, 0,") then Character.Humanoid.WalkSpeed = RunningSpeed Character.Humanoid.JumpPower = 50 Character.Animate.Disabled = true Character.Animate2.Disabled = false end end end end) UIS.InputEnded:Connect(function(input) if input.KeyCode == Enum.KeyCode.LeftShift then if Character.Humanoid.Jump == false then if Character.Humanoid.MoveDirection == ("0, 0, 0,") then Character.Humanoid.WalkSpeed = WalkingSpeed Character.Humanoid.JumpPower = 50 Character.Animate.Disabled = false Character.Animate2.Disabled = true end end end end)

instead of detecting if player is standing still it stops the entire script from working

0
MoveDirection is a Vector3, not a string. DeceptiveCaster 3761 — 3y

1 answer

Log in to vote
1
Answered by 3 years ago
Edited 3 years ago

There are multiple ways to detect if a character is standing still. The third method is the best since it doesn't consume as much memory as the first and neither needs to check the state of the character to determine if the player is idling as in the second method.

1) You could use MoveDirection to check if the humanoid.MoveDirection.Magnitude > 0. If it's higher, it means the humanoid is moving. Example:

-- Services
local RunService = game:GetService("RunService")
-- Variables
-- Since this is a local script and it's located in StarterCharacterScripts, it gets put inside the player when the game starts. So you can get the character of the player using script.Parent
local char = script.Parent
local hum = char:WaitForChild("Humanoid")
-- Functions

local function CharIsIdle()
    if hum.MoveDirection.Magnitude > 0 then
        print("Hum is running")
    else
        print("Hum is idling")
    end
end

RunService.RenderStepped:Connect(CharIsIdle)

*Edit: adding the other method you can use:

2) This next method will fire whenever the humanoid starts running. By default, hum.Running sends the speed of the character to the function that connects to it.

So we will check if the speed of the character is higher than 1. If it is higher, then the character is running. If it is lower, then the character is idling. I put it 1 so that it only fires when the character actually has some speed rather than 0 since the character speed for some reason may get higher than 0 but yet is idling.

But since the character can jump or freefall with a speed of 0 (jumping or freefalling in place), we want to get the state of the humanoid to confirm it isn't actually doing either of those first before checking it's speed.

Next, to make sure the scripts only runs once when the character is idling, we add a variable named "isIdling" that is global to the HumIsRunning function but local to the script (this way the variable doesn't reset to false whenever the HumIsRunning function gets called). Now whenever we detect that the humanoid is running or jumping or freefalling we change the variable isIdling to false. And whenever the character is idling, we change the variable to true. Next time if our script confirms that the character is idling, it will check if the variable is not set to true. Only if it's not set to true will it run the subsequent idling scripts. This will prevent the script from running the idling part of the script for more than once unless of course the speed of the character changes higher than 0 then it will run it again next time it idles.

Here's how:

-- Since this is a local script and it's located in StarterCharacterScripts, it gets put inside the player when the game starts. So you can get the character of the player using script.Parent

-- Getting char and humanoid
local char = script.Parent
local hum =  char:WaitForChild("Humanoid")
-- Variables
local isIdling = true
-- Functions
local function HumIsRunning(speed)
    local state = hum:GetState()
    -- Check if the character is not jumping or freefalling (They can jump and freefall with a speed of 0)
    if (state ~= Enum.HumanoidStateType.Freefall) and (state ~= Enum.HumanoidStateType.Jumping) then
        if speed > 1 then
            -- Character is not idling
            isIdling = false
            print("Hum is running")
        else
            -- Character is idling
            if not isIdling then
                isIdling = true
                print("Hum is idling")
            end
        end
    end

    -- We want to change the idling variable to false if the character jumps or freefalls
    if (state == Enum.HumanoidStateType.Freefall) or (state == Enum.HumanoidStateType.Jumping) then
        isIdling = false
    end
end
-- Creating Connections
hum.Running:Connect(HumIsRunning)

Now after everything we've done, if the character is idling it will only run once the subsequent lines and not multiple times like the last method. Although, you could do the same thing with the 1) method and add a boolean variable that changes whenever the char is running or idling so that it runs the script inside either only once.

3) The third method is pretty similar to the first but in this case you check the velocity of the HumanoidRootPart of the character. If it's higher than 1 then the humanoid is running/not idling. If it's lower, then it's idling. We will listen to the humanoid state and whenever it get fired, we connect it to our function. This method doesn't need us to check if the character is freefalling or jumping since if they are, their velocity.Magnitude would be higher than 1

Here's how:

-- Since this is a local script and it's located in StarterCharacterScripts, it gets put inside the player when the game starts. So you can get the character of the player using script.Parent
-- Getting char and humRootPart of player
local char = script.Parent
local hum = char:WaitForChild("Humanoid")
local humRootPart = char:WaitForChild("HumanoidRootPart")
-- Variables
local isIdling = true
-- Functions
local function GetCharState()
    if humRootPart.Velocity.Magnitude >= 1 then
        if isIdling then
            isIdling = false
            print("Character is running")
        end
    else
        if not isIdling then
            isIdling = true
            print("Character is idling")
        end
    end
end

hum.Running:Connect(GetCharState)
Ad

Answer this question