Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Part still damages localplayer?

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?

2 comments

0

is this in a local script?

0

no

3 answers

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.

1 comment

0

just to remind. this is not a localscript

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.

2 comments

0

This is easy but i want it for script. not for localscript

0

oh, you can use GetPlayerFromCharacter in this case, Link: https://developer.roblox.com/en-us/api-reference/function/Players/GetPlayerFromCharacter

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?

This discussion is preserved as it was. New answers, comments, and votes are closed.