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?
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.
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.
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?