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

How can i make this script not to damage the localplayer?

How can i make this script not to damage the localplayer, this is a script btw not a localscript, Or not touchable from localplayer!

local buttonPressed = false

script.Parent.Touched:Connect(function(x)
	if(x.Parent:FindFirstChild("Humanoid")) and not buttonPressed then
		buttonPressed = true
		print("Pass2")
		x.Parent.Humanoid:TakeDamage(10)
		wait(1)
		buttonPressed = false
	end
end)

1 answer

Check if the touched part's ancestor is not owned by the localplayer by using a combination of GetPlayerFromCharacter and FindFirstAncestorWhichIsA.

userdata:GetPlayerFromCharacter returns a player object that we can compare with the Local Player. FindFirstAncestorWhichIsA finds the first instance that is the ancestor of the userdata.

script.Parent.Touched:Connect(function(x)
	local Humanoid = x.Parent:FindFirstChildWhichIsA('Humanoid', true)
	local Ancestor = x:FindFirstAncestorWhichIsA('Model')
	if (not buttonPressed and Humanoid and game:GetService('Players'):GetPlayerFromCharacter(Ancestor) ~= game.Players.LocalPlayer) then
	--code
	end
end);

5 comments

0

Not sure if you saw, but this isn't a LocalScript.

0

oops, didn't read that part. is the script in a tool? if so, use the tool's parent. when equipped, tools are moved into the character.

0

if this is a script in startercharacterscripts or something, these are also moved into the character, so you can use script.Parent

0

this script is on a part, this script doesn't work.

0

dude, you can't mention damaging the local player and then say the script isn't a localscript. also, please don't edit the post whenever someone gets confused over a piece of information. state everything and think about what you're going to post before you hit send. this is becoming much more convoluted because you fail to do that.

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