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

Part still damages localplayer?

Asked by
imKlevi 94
4 years ago
Edited 4 years ago

I want this script not to dmg my local player! (not localscript)

script.Parent.Touched:Connect(function(hit)
local hum = hit.Parent:FindFirstChild("Humanoid')
if hum and not game.Players.LocalPlayer then
hum:TakeDamage(10)
 else
print("This is local player")
end
end)
-- It still damages local player, whats wrong?
0
is this in a local script? ForeverBrown 356 — 4y
0
no imKlevi 94 — 4y

3 answers

Log in to vote
0
Answered by 4 years ago

In the if statement, you are checking if the humanoid exists which is correct but the next condition is not what you want.

if hum and not game.Players.LocalPlayer then

The condition not game.Players.LocalPlayer is checking if the LocalPlayer doesn't exist, not if the player that touched is the Local Player. Since the Local Player does exist, it will run regardless of whatever player touches.

Here's your fix:

if hum and hum.Parent ~= game.Players.LocalPlayer.Character then

This if statement will check if the parent of hum is the Local Player's Character.

0
just to remind. this is not a localscript imKlevi 94 — 4y
Ad
Log in to vote
0
Answered by
ArtBlart 533 Moderation Voter
4 years ago

Simply calling not game.Players.LocalPlayer will not work. My suggestion is to use the hit argument in your Touched function like so:

script.Parent.Touched:Connect(function(hit)
    local hum = hit.Parent:FindFirstChild("Humanoid')
    if hum and hit.Parent ~= game.Players.LocalPlayer.Character then
        hum:TakeDamage(10)
    else
        print("This is local player")
    end
end)

I haven't tested this, but if it ends up erroring out you can always check the name of both. The LocalPlayer's name is always the same as the Character's.

0
This is easy but i want it for script. not for localscript imKlevi 94 — 4y
0
oh, you can use GetPlayerFromCharacter in this case, Link: https://developer.roblox.com/en-us/api-reference/function/Players/GetPlayerFromCharacter ArtBlart 533 — 4y
Log in to vote
0
Answered by
Robowon1 323 Moderation Voter
4 years ago
Edited 4 years ago

You are attempting to reference LocalPlayer from a server script, a fix to this:


script.Parent.Touched:Connect(function(hit) local hum = hit.Parent:FindFirstChild("Humanoid') if hum and game.Players:FindFirstChild(hum.Name) == nil then hum:TakeDamage(10) else print("This is local player") end end) -- It still damages local player, whats wrong?

Answer this question