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

how can I tell if a player is actively touching a block?

Asked by 2 years ago

I know how to use the touch event and tell when a player touches a block but I need to know how to tell if a player is still standing on the block or not

2 answers

Log in to vote
0
Answered by 2 years ago

you can use the touched event:

local brick = script.Parent  --getting variables for the brick

brick.Touched:Connect(function(hit) --Checking if the brick is touched
    if hit.Parent:FindFirstChild("Humanoid") then --Checking if touched part has a humanoid (player)
        hit.Parent.Humanoid.Health = 0 --player dies
    end
end)
Ad
Log in to vote
0
Answered by
sleazel 1287 Moderation Voter
2 years ago
Edited 2 years ago

So there is no easy solution for this I am afraid. Once players stops moving, touched events are not fired anymore and using PartsInRegion3 is very costly.

IMHO the best way is to use periodic check of magnitude of vector3 to tell how far character is from the block combined with touch event that will initialize such checks. Once player leaves the block, the check loop will cease.

Make sure that this is a server script.

local part = script.Parent
local PlayersNearThePart = {}
local approxPartRadius = (part.Size.X/2 + part.Size.Z/2)/1.5

part.Touched:Connect(function(hit)

    local root = hit.Parent:FindFirstChild("HumanoidRootPart")

    if not root then return end -- not a humanoid

    local plr = game.Players:GetPlayerFromCharacter(hit.Parent)

    if not plr then return end -- could be an NPC, or the player left

    if PlayersNearThePart[plr] then return end -- player has already been registered

    PlayersNearThePart[plr] = true
    --do something when player entered
    -- you can check at anytime 
    --if PlayersNearThePart[plr] is set to see if player is still on the part

    --periodic check to see if player left the part
    while root.Parent.Parent and (root.Position - part.Position).Magnitude < approxPartRadius then
        task.wait(1)
    end

    --player left, clear the register
    PlayersNearThePart[plr] = nil

end)

I hope this helps.

Answer this question