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

How Would I Make an Object That's a Part of a Player Damage Other Players and Not The Player?

I have a part that is in the player that I want to damage other players if they come in contact with it without ever hurting the player. So I made this script for it but as it doesn't damage the main payer, it doesn't damage any players at all for some reason. Any help? And it's in a normal script, just in case that's causing the problem.

local player = script.Parent.Parent
local nameL = player.Name
local blast = script.Parent

blast.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") then
		if hit.Parent.Name ~= nameL then
			hit.Parent.Humanoid.Health = hit.Parent.Humanoid.Health-50
		end
	end
end)

7 comments

0

Does Anyone like The answer I gave? If you do please Accept this answer lol! Im not used to getting accepted X:D!

0

You spelled Humanoid wrong but I'll accept it.

0

oh ok im sorry lol

0

Its not working??? my Script? What is the Error?

0

Its not working??? my Script? What is the Error?

0

I still don't know myself how to find the local player through a normal script and that's the error script.Parent.Parent gives me.

0

you simply can't do that

2 answers

✓ Accepted answer

Use this script!

and before you read this I want you to read what I have to say lol!

so I use a method called "ehum" (the abbreviation for it is "EnemyHumanoid")

so an enemy - humanoid is a humanoid that is not you humanoid! This works well if you have a weapon that you only want foes' to take damage on so here is the script!!!

------------i'd Prefer you use a script and not a local script.

local object = ---YOUR OBJECT HERE!
local player = script.Parent.Parent---Please get your player so we can get the character!
local char = player.Character--your character (you can get it in "ANY" way!)
local hithum = false --- a debounce! (if needed!)
local hum = char.Huamnoid -- We will also need the humanoid For this script!

object.Touched:Connect(function(hit)

local ehum = hit and hit.Parent:FindFirstChild("Humanoid") or hit.Parent.Parent:FindFindFirstChild("Humanoid") --Just incase it hits a hat or some other accessory lol!

if ehum and ehum ~= hum then--This is the Important Part! This part allows only the other humanoid that is not equal to YOUR Humanoid to get hit!
if hithum == false then
hithum = true-----So It doesn't hit the Person a Million TIMES!


ehum:TakeDamage(50)---Make it Take Damage!
wait(2)--this is if you want it to be hit every 2 seconds! you can change this if you want!
hithum = false
end
end
end)

also never use heath as a value to take away the health!

all ways use the :TakeDamage() Method while dealing with Health

using heath - heath is bad because heath regenerates and goes down at weird pace.

----T40

2 comments

0

It's not working.

0

The reason why it isn't working is because the player is never the grandparent of the script. Line 4 is wrong for this reason.

Here is the actual solution to your problem (this is a regular script):

local blast = script.Parent
local bool = true
blast.Touched:Connect(function()
	if not bool then return end -- Debounce
	bool = false
	if blast:FindFirstChild("Humanoid") then
		local player = game.Players:GetPlayerFromCharacter(blast) -- This is where you find the player, not script.Parent.Parent
		if player then
			local hum = blast:FindFirstChild("Humanoid")
			if hum.Health == 0 then return end -- Another debounce, this prevents the Humanoid from having negative Health
			hum.Health = hum.Health - 50
		end
	end
	wait(3) -- The waiting time can be changed
	bool = true
end)

Take notice of line 11: hum.Health = hum.Health - 50. The reason why TakeDamage() is not being used is because it can result in a negative (or exceedingly positive) amount of HP, causing all sorts of bugs in your game (which is countered by line 10). (What I mean is that if you did TakeDamage(-100), for instance, the MaxHealth of that Humanoid is exceeded.)

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