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

.Touched function doesn't work for me when I'm trying to deal damage to others, help?

So basically, I've been making this attack script and I really need this damage thing to work, here's what I got right now, it's meant to work but doesn't. Can anyone please hint out the mistake?

part.Touched:connect(function(hit)
if hit.Parent:FindFirstChild("Humanoid") ~= nil then
if hit.Parent.Name ~= Player.Name then
hit.Parent.Humanoid:TakeDamage(math.huge)
end
end
end)

2 answers

✓ Accepted answer

Is this what you meant?

local playername = "tantec" -- name of the player NOT to deal damage to.

script.Parent.Touched:Connect(function(hit)
	for i,v in pairs(game.Players:GetPlayers())do
		if v and v.Character and v.Character:IsAncestorOf(hit) then
			if v.Name:lower() ~= playername:lower() then
				v.Character:BreakJoints()
			end
		end
	end
end)

part should be defined so maybe put the script under the part and use script.Parent

don't use :connect instead use :Connect

=nil is also not needed and you shouldn't use **hit.Parent=Player.Name** instead, just use if hit.Parent:FindFirstChild("Humanoid") then

math.huge is an infinite value so you can just do max damage or a certain integer hit.Parent.Humanoid.Health = 0

your script should end up like

script.Parent.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild("Humanoid") then
hit.Parent:FindFirstChild("Humanoid").Health = 0
end
end)

3 comments

0

I don't get it. Why do people keep complaining about using :connect or :Connect? It's a RBXSignal, what diffirences are there between those two?

0

None. One's deprecated. Not really an issue.

0

I don't think I've rephrased this properly, basically the original script was meant to kill others and not myself, plus the script that I'm using is for script builder so I can't do it like that. Also, the script works on a part that's not touching me so I think the problem is that it's only getting my name and no one else because it spawns from me

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