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

How do I make a script that activates when a player stops moving?

Asked by 10 years ago

I attempted to make a script that activates and deactivates when a player stops moving/ starts moving respectively. This is my script. (Yes it's a local script)

local p = game.Players.LocalPlayer
repeat wait() until p.Character
local c = p.Character
local lastPos
local curPos
local dist

c.Torso.Changed:connect(--Whenever the player moves
function()
    curPos = c.Torso.Position -- Current position
    if lastPos and dist and dist < 1 then
        wait(2)
        if dist < 1 then -- If they're still in the same spot..
            --Code here
        end
    end
    lastPos = curPos -- Last spot
    dist = (curPos-lastPos).magnitude -- Distance between last spot and current spot
end
)

I don't quite understand this kind of scripting, could someone help me out with it?

2 answers

Log in to vote
2
Answered by
Ekkoh 635 Moderation Voter
10 years ago

The Changed event of Parts doesn't get fired when Position changes. You'd use the Running event of a Humanoid to detect when the character has stopped walking. For example-

local Humanoid = ... -- location of humanoid here

Humanoid.Running:connect(function(speed)
    if speed <= 0 then
        print("Character has stopped walking.")
        -- stop code here
    else
        print("Character has begun to walk.")
        -- move code here
    end
end)
Ad
Log in to vote
-2
Answered by 10 years ago

I fixed most of it

local p = game.Players.LocalPlayer
repeat wait() until p.Character
local c = p.Character
local lastPos
local curPos
local dist
c.Torso.CFrame.Changed:connect( function() -- Added CFrame because Its the position
    curPos = c.Torso.CFrame-- Current position
    if lastPos and dist and dist < 1 then
        wait(2)
        if dist < 1 then -- If they're still in the same spot..
            --Code here
        end
    end
    lastPos = curPos -- Last spot
    dist = (curPos-lastPos).magnitude -- Distance between last spot and current spot
end
)
0
This would not work. CFrame does not have a Changed event. Please don't reply if you don't know what you're doing. Ekkoh 635 — 10y

Answer this question