Fire an event when the position of the character changes

What I’m trying to do is fire an event or a function every time the character moves .2 studs. Any solutions

2 Likes

Couldn’t you just use a GetPropertyChangedSignal on the character’s root part’s position?

character.HumanoidRootPart:GetPropertyChangedSignal("Position"):Connect(function()
	-- do stuff
end)

Just tried that, doesn’t work…

1 Like

You can use the MoveDirection property of the Humanoid and use GetPropertyChangedSignal.

1 Like

Please follow the guidelines for this category and restructure your post accordingly. Your post should contain a description of your use-case so we can better answer your question, it is currently too broad, and perhaps we can respond with a better solution if we know what you are trying to achieve in the wider scheme of things.

Try for a solution of using Humanoid.Running event.

local lastPos
Humanoid.Running:Connect(function()
	if not lastPos then
		lastPos = character.HumanoidRootPart.Position
	end

	if (character.HumanoidRootPart.Position - lastPos).Magnitude > 0.2 then
		-- do something
		lastPos = character.HumanoidRootPart.Position
	end
end)

Oh man, no Studio access really sucks.


Post is very vague of what you are trying to achieve here.

3 Likes

I just tried a new method, though it will only work on changes from the humanoid and not environmental changes:

local RunService = game:GetService("RunService")

local char = script.Parent

local noid = char:WaitForChild("Humanoid")

local speed = 0

noid.Running:Connect(function(spd)
	speed = spd
end)

local i = 0
RunService.Heartbeat:Connect(function(dt)
	i = i + dt * speed
	if i > 0.2 then
		print("over 0.2")
		i = 0
	end
end)

You could probably also just use magnitude checks every heartbeat:

local RunService = game:GetService("RunService")

local char = script.Parent

local root = char:WaitForChild("HumanoidRootPart")

local lastPos = root.Position
RunService.Heartbeat:Connect(function()
	local newPos = root.Position
	if (newPos - lastPos).Magnitude > 0.2 then
		print("over 0.2")
		lastPos = newPos
	end
end)

@Operatik, the Running event only fires when the speed of the humanoid changes, not every frame that the humanoid is moving, which means you can technically run for a very long time after reaching the 0.2 stud mark with that method. Sorry if that was blatant :sweat_smile:

I just figured out that Running event only runs once and perhaps the checking using RunService actually works. Simply alternate the event as according if the Running event does not work.

1 Like

sry if it was to vague and thx for the help

1 Like