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?
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 )