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

How do I make an onTouch command only visible for the player who touched the part first?

Asked by 3 years ago
Edited 3 years ago

So I have an onTouch Script and if someone touches it the event will happen for all users. How do I make it so that only the player who touched the part with the onTouch script can see the event. The script I use for onTouch Events:

local function onTouch(hit)
[Code goes here]
end

script.Parent.Touched:Connect(onTouch)
0
Please provide more information about what you wanted to do after the player touches the part WoTrox 345 — 3y
0
I want to destroy something but that it only destroys for the player who touched the Part with the Script but not for the other players Crashbandicoot9024 2 — 3y

1 answer

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

Edit: Referring to your comment, you forgot to put an "end" to close the function and you would also have to check if it's a player

function onTouch(hit)
    if hit.Parent:FindFirstChild("Humanoid)" then
    --code here
    end
end

script.Parent.Touched:Connect(onTouch)

If you mean all players (game.Players) then

function onTouch(hit)
    if hit.Parent:FindFirstChild("Humanoid") then --checks if it's a real player
        for i,v in pairs(game.Players:GetPlayers()) do --goes through the list of game.Players
        --code here (use v for the players in game.Players) 
        end
    end
end

script.Parent.Touched:Connect(onTouch)

But if you mean all of the character's players then

function onTouch(hit)
    if hit.Parent:FindFirstChild("Humanoid") then --checks if it's a real player
        for i,v in pairs(game.Players:GetPlayers()) do --goes through the list of game.Players
        v.Character -- add code here
        --example: v.Character.Humanoid.Health = 0
        end
    end
end

script.Parent.Touched:Connect(onTouch)
0
I actually mean that it affects all player but I want that it only affects the player who touched the part with the onTouch Script. Crashbandicoot9024 2 — 3y
0
Alright I edited my answer KripticalYT 100 — 3y
0
I only forgot to put it in the title but I always add end after the script so I edited it. Crashbandicoot9024 2 — 3y
Ad

Answer this question