Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How to detect a player not touching a block anymore?

Asked by 5 years ago

How would I detect a player leaving a block if I want something to keep happening while the player is standing on that block. Here is my touch script:

script.Parent.Touched:Connect(function(hit)
hit.Parent .. "touched this block"
end)

1 answer

Log in to vote
0
Answered by 5 years ago

You can detect when a player is no longer touching a block by using the .Touched event.

An example would be:

local part = script.Parent;
local Players = game:GetService("Players");

part.Touched:Connect ( function (hit)
    local humanoid = hit.Parent:FindFirstChildOfClass("Humanoid");
    if humanoid then
        local player = Players:GetPlayerFromCharacter(hit.Parent); 
        print(player.Name, "is touching the part");
    end
end)

part.TouchEnded:Connect ( function (hit)
    local humanoid = hit.Parent:FindFirstChildOfClass("Humanoid");
    if humanoid then
        local player = Players:GetPlayerFromCharacter(hit.Parent);
        print(player.Name, "is no longer touching the part");
    end
end
Ad

Answer this question