How can I easily detect if someone is standing on a part without moving?

Hello!

I’m trying to make a button that activates a kill script in a part, that will kill anyone standing on it. And the problem is that it only kills the player if it is moving, and not when it is standing still.

This is the script I use, that gets enabled (via properties) when a button is pressed:

script.Parent.Touched:Connect(function(hit)

     if hit.Parent:FindFirstChild("Humanoid") then

         local humanoid = hit.Parent.Humanoid

         humanoid.Health = 0

     end

end)

I’ve used a Remote Event to activate it, so that can’t be the issue.

4 Likes

Touched fires when the part begins to touch another, therefore obviously they’ll be moving.
I’d recommend you use :GetTouchingParts() to get any parts touching your part.
If you find a Humanoid in the part.Parent then check it’s MoveDirection, this’ll be 0,0,0 if they’re not moving.

For example:

while true do --// Loop
    wait() --// Yield to ensure it doesn't cause a game script timeout
    local parts = script.Parent:GetTouchingParts() --// Get every part touching script.Parent
    for _, touchingPart in ipairs(parts) do --// Iterate through
        local humanoid = touchingPart.Parent:FindFirstChild("Humanoid") --// Find a Humanoid in the part's Parent
        if humanoid and humanoid.MoveDirection == Vector3.new(0,0,0) then --// If the Humanoid exists and it's not moving
            humanoid.Health = 0 --// Kill the Humanoid
        end
    end
end
7 Likes