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 2 years ago
Edited 2 years 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.

01game.Players.LocalPlayer.Character.Humanoid.WalkSpeed = 1
02 
03 
04while true do  
05    local speed = game.Players.LocalPlayer.Character.Humanoid.WalkSpeed
06    if speed > 0 then
07        local Random = math.random(1,100)
08        if Random < 99 then
09            speed = speed + math.random(1,3)
10        else
11            speed = speed - speed
12        end
13    else
14        --This is where I want it to detect (I only want it to be able to detect here)
15    end
16    wait(1)
17end

1 answer

Log in to vote
0
Answered by 2 years ago
Edited 2 years ago

You can detect player touching like this:

01local player = game.Players.LocalPlayer
02local character = player.Character
03detected = {}
04if character ~= nil then
05    for _, part in pairs(character.HumanoidRootPart:GetTouchingParts()) do
06        if game.Players:GetPlayerFromCharacter(part.Parent) ~= nil then
07            if (detected[game.Players:GetPlayerFromCharacter(part.Parent)] == nil) and (part.Parent ~= character) then
08                detected.insert((game.Players:GetPlayerFromCharacter(part.Parent) = true), #detected)
09                print("Player touch detected: ", game.Players:GetPlayerFromCharacter(part.Parent))
10            end
11        end
12    end
13end

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 — 2y
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 — 2y
0
Thanks! Puppy_lovertheawsome 84 — 2y
Ad

Answer this question