Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 5 years ago

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)

0
Does Anyone like The answer I gave? If you do please Accept this answer lol! Im not used to getting accepted X:D! Tizzel40 243 — 5y
0
You spelled Humanoid wrong but I'll accept it. songboy50 77 — 5y
0
oh ok im sorry lol Tizzel40 243 — 5y
0
Its not working??? my Script? What is the Error? Tizzel40 243 — 5y
View all comments (3 more)
0
Its not working??? my Script? What is the Error? Tizzel40 243 — 5y
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. songboy50 77 — 5y
0
you simply can't do that DeceptiveCaster 3761 — 5y

2 answers

Log in to vote
0
Answered by
Tizzel40 243 Moderation Voter
5 years ago
Edited 5 years ago

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

0
It's not working. songboy50 77 — 5y
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. DeceptiveCaster 3761 — 5y
Ad
Log in to vote
0
Answered by 5 years ago

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

Answer this question