Ad
Log in to vote
0

How do i detect when a player stops walking? awnser ASAP please

Asked by 21 days ago

Ok so i am making a animation pack and i need to know when the player stops walking this is what i have so far

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

local RS = game:GetService("ReplicatedStorage")
local RStop = RS.RStoppedWalking

local Player = game.Players.LocalPlayer
local PlayerC = Player:GetChildren()
local Character = PlayerC.Character
local Humanoid = PlayerC:WaitForChild("Humanoid")
local VectorZero = Vector3.new(0, 0, 0)
local Walking = false

while true do
    wait()
    print("looping")
    if Humanoid.MoveDirection ~= VectorZero then
        Walking = true
    else
        Walking = false
    end 
end 

if Walking == false then 
    print("If walk = false")
    RStop:FireServer(Walking)
end


it is returning a error that says nil value ik its something to do with humanoid and i cant figure it out my discord is bily117#5405 if u know anything please dm me or comment or awnser the question

2 answers

Log in to vote
1
Answered by 21 days ago
Edited 21 days ago

The error you're getting is because you're getting the Character incorrectly. You don't need to get the player's children to get the character because the character isn't a child of the player, it's a property that contains a reference to the character model which is parented to the workspace. So, you can just do Player.Character.

Also, your code is not going to work properly because your if statement is outside of the loop and is not being constantly checked by anything. Also, because you have a while loop, it's never actually going to get to the if statement because it's looping indefinitely.

You don't need a while loop for this though, you can use the function GetPropertyChangedSignal to detect when the MoveDirection property has changed. This is a lot better way of doing this because it won't constantly use resources when nothing is being changed.

I also find Attributes to be very helpful in these types of situations because attributes, unlike variables, you can detect when a specific attribute has changed by using the function GetAttributeChangedSignal.

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

local VectorZero = Vector3.new(0, 0, 0)

Character:GetAttributeChangedSignal("Walking"):Connect(function()

    if Character:GetAttribute("Walking") then
        print("Player is walking")
    else
        print("Player has stopped")
    end
end)

Humanoid:GetPropertyChangedSignal("MoveDirection"):Connect(function()

    if Humanoid.MoveDirection ~= Vector3.new(0,0,0) then
        Character:SetAttribute("Walking", true)
    else
        Character:SetAttribute("Walking", false)
    end
end)

You could also try using the Humanoid.Running event which does exactly the same as the code above. This event has a parameter which is the speed the character is moving at, which you could use to detect when they're standing still, running, etc...

Example

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

Humanoid.Running:Connect(function(Speed)

    if Speed > 0 then
        print("Player is moving")
    else
        print("Player has stopped")
    end
end)
Ad
Log in to vote
0
Answered by 18 days ago

There is a very simple and easy way to detect if a player is moving or not. It's called Magnitude and it's located in the humanoid. For example, I use Magnitude in my run scripts.

 local UIS = game:GetService('UserInputService')
local Player = game.Players.LocalPlayer
local Character = game.Players.LocalPlayer.Character

UIS.InputBegan:Connect(function(input)--When a player has pressed LeftShift it will play the animation and it will set the normal walking speed (16) to 35.
    if input.KeyCode == Enum.KeyCode.LeftShift then
        **if Character.Humanoid.MoveDirection.Magnitude > 0 then**
            Character.Humanoid.WalkSpeed = 26
            local Anim = Instance.new('Animation')
            Anim.AnimationId = 'rbxassetid://7162914367'
            PlayAnim = Character.Humanoid:LoadAnimation(Anim)
            PlayAnim:Play()
        else
            print("The player is not moving.")
        end

    end
end)

UIS.InputEnded:connect(function(input)
    if input.KeyCode == Enum.KeyCode.LeftShift then
        Character.Humanoid.WalkSpeed = 16
        PlayAnim:Stop()
    end
end)

In your script, It could be...

local Character = game.Players.LocalPlayer.Character
local RS = game:GetService("ReplicatedStorage")
local RStop = RS.RStoppedWalking

if Character.Humanoid.MoveDirection.Magnitude > 0 then
    RStop:FireServer(Walking)
end)

Answer this question