How can I detect player movement?

Hello. I am trying to make a game where at certain times a part can kill you if you walk on it, but at some other times its completely safe. Also if you walk on it while it’s safe and then it becomes unsafe, it should NOT kill you. Like red light green light. The method of killing people is using a script inside the part that can either be enabled or disabled, with a very basic kill script shown below:

script.Parent.Touched:Connect(function(hit)
	if players:GetPlayerFromCharacter(hit.Parent) then
		if hit.Parent:FindFirstChild("Humanoid") then
			hit.Parent.Humanoid.Health = 0
		end
	end
end)

When a player is on this while it is unsafe (they get on here while it is safe), they still get killed for some reason. I believe this is because of idle animations. I don’t know how to fix this. Please help me. Thanks.

you can check the players humanoids movedirection property

if Humanoid.MoveDirection.Magnitude == 0 then 
	print("player not moving")
else 
	print("player moving")
end

or

Humanoid:GetPropertyChangedSignal("MoveDirection"):Connect(function()
	if (Humanoid.MoveDirection.Magnitude == 0) then 
		print("player not moving")
	else 
		print("player stopped moving")
	end
end)

I used the second method… it seems to work but whenever the script is disabled it still keeps kiling the player for some reason.

script.Parent.Touched:Connect(function(hit)
	if players:GetPlayerFromCharacter(hit.Parent) then
		if hit.Parent:FindFirstChild("Humanoid") then
			if hit.Parent.Humanoid.MoveDirection.Magnitude ~= 0 then
				hit.Parent.Humanoid.Health = 0
			end
		end
	end
end)

try doing something like this:

local TouchedConnection = nil
local Activate = function()
	TouchedConnection = script.Parent.Touched:Connect(function(hit)
		if players:GetPlayerFromCharacter(hit.Parent) then
			if hit.Parent:FindFirstChild("Humanoid") then
				if hit.Parent.Humanoid.MoveDirection.Magnitude ~= 0 then
					hit.Parent.Humanoid.Health = 0
				end
			end
		end
	end)
end

local Disable = function()
	TouchedConnection:Disconnect()
end


--// test code below

while true do 
	Activate()
	print("activated")
	task.wait(4)
	
	Disable()
	print("disabled")
	task.wait(4)
end

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.