Detect player movement

so I’ve been working on this for a while now and have done a lot of googling and searching through the developer hub and found nothing to solve my problem which is:
if a player touches a part while moving the player dies

my ideas were user input service and touched events and using Humanoid.FloorMaterial but my efforts were in vain
note that I want to use this script for multiple parts.

thanks for your help!

1 Like

You could say:

local player = game.Players.LocalPlayer
local char = player.Character
if char:WaitForChild("HumanoidRootPart").CFrame = Vector3.new(1,0,0) then
    --code goes here
end
1 Like

if you have all your parts in one parent you can loop through all of them like so

for i,v in pairs(YourParts:GetChildren()) do 
if v:IsA("BasePart") then
v.Touched:Connect(function(hit)
local char = hit.Parent
local player = game.Players:GetPlayerFromCharacter(char)
if char:FindFirstChild("Humanoid").MoveDirection.Magnitude > 0 then
char.Humanoid.Health = 0
end
end)
end
end
2 Likes

there is an error message that sometimes occurs:
attempt to index nil with ‘MoveDirection’

try this

for i,v in pairs(YourParts:GetChildren()) do 
if v:IsA("BasePart") then
v.Touched:Connect(function(hit)
local char = hit.Parent
local player = game.Players:GetPlayerFromCharacter(char)
local Humanoid = char:FindFirstChild("Humanoid")
if Humanoid and Humanoid.Health > 0 then
if Humanoid.MoveDirection.Magnitude > 0 then
char.Humanoid.Health = 0
end
end
end)
end
end
2 Likes

updated the code, added a check if the player is alive while touching the parts.

1 Like