Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

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

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

✓ Accepted answer

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)

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
)

1 comment

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.

This discussion is preserved as it was. New answers, comments, and votes are closed.