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