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

how do detect when a player is touched by another player?

Asked by 1 year ago
Edited 1 year ago

I am just wondering how I could detect that. I am also wondering if that is possible inside of a local script in StarterPlayer > StarterCharacterScripts. I don't know if you want the code so I will just show it and note where I want it to be able to detect.

game.Players.LocalPlayer.Character.Humanoid.WalkSpeed = 1


while true do   
    local speed = game.Players.LocalPlayer.Character.Humanoid.WalkSpeed
    if speed > 0 then
        local Random = math.random(1,100)
        if Random < 99 then
            speed = speed + math.random(1,3)
        else
            speed = speed - speed
        end
    else
        --This is where I want it to detect (I only want it to be able to detect here)
    end
    wait(1)
end

1 answer

Log in to vote
0
Answered by 1 year ago
Edited 1 year ago

You can detect player touching like this:

local player = game.Players.LocalPlayer
local character = player.Character
detected = {}
if character ~= nil then
    for _, part in pairs(character.HumanoidRootPart:GetTouchingParts()) do
        if game.Players:GetPlayerFromCharacter(part.Parent) ~= nil then
            if (detected[game.Players:GetPlayerFromCharacter(part.Parent)] == nil) and (part.Parent ~= character) then
                detected.insert((game.Players:GetPlayerFromCharacter(part.Parent) = true), #detected)
                print("Player touch detected: ", game.Players:GetPlayerFromCharacter(part.Parent))
            end
        end
    end
end

This code generates the table (detected), which contains a list of all players who are currently touching the player.

0
When I tried this the player detects itself. How do I stop that? Puppy_lovertheawsome 84 — 1y
0
edited the script to fix this. all that needed to happen is check if the part that has touched the player is in the player or not. sergeant_ranger 184 — 1y
0
Thanks! Puppy_lovertheawsome 84 — 1y
Ad

Answer this question