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

Gun script isnt damaging when it hits a players accessory?

Asked by 7 years ago
Edited 7 years ago

I have been extremely modifying a gun script and the last thing that I have to change is so when a bullet hits a players accessory it still damages them.

But at the moment it doesnt so, lets say a player has a backpack on. If they get hit in the backpack nothing happens.

Heres the code, these are just 2 chunks of the code, but its the part that does the damage.

LOCAL SCRIPT

if not Module.ExplosiveEnabled then
            if Hit and Hit.Parent then
                local TargetHumanoid = Hit.Parent:FindFirstChild("Humanoid")
                local TargetTorso = Hit.Parent:FindFirstChild("UpperTorso")
                local TargetAddon = Hit.Parent:FindFirstChildOfClass("Accessory")
                --local TargetTEAM = Hit.Parent:FindFirstChild("TEAM")
                if TargetHumanoid and TargetHumanoid.Health > 0 and TargetTorso and TargetAddon then
                    --if TargetTEAM and TargetTEAM.Value ~= TEAM.Value then
                        InflictTarget:FireServer(TargetHumanoid,
                                                TargetTorso,
                                                TargetAddon,
                                                (Hit.Name == "Head" or Hit.Name == "Helmet" and Module.HeadshotEnabled) and Module.BaseDamage * Module.HeadshotDamageMultiplier or Module.BaseDamage,
                                                Direction,
                                                Module.Knockback,
                                                Module.Lifesteal,
                                                Module.FlamingBullet)
                        PiercedHumanoid[TargetHumanoid] = true
                    --end
                else
                    PierceAvailable = 0
                end
            end
        else

SERVER SCRIPT

InflictTarget.OnServerEvent:connect(function(Player,TargetHumanoid,TargetTorso,TargetAddon,Damage,Direction,Knockback,Lifesteal,FlamingBullet)
    if Player and TargetHumanoid and TargetHumanoid.Health ~= 0 and TargetTorso and TargetAddon then
        while TargetHumanoid:FindFirstChild("creator") do
            TargetHumanoid.creator:Destroy()
        end
        local creator = Instance.new("ObjectValue",TargetHumanoid)
        creator.Name = "creator"
        creator.Value = Player
        game.Debris:AddItem(creator,5)
        TargetHumanoid:TakeDamage(Damage)  
        if Knockback > 0 then
            TargetTorso.Velocity = Direction * Knockback
        end
        if Lifesteal > 0 and Humanoid and Humanoid.Health ~= 0 then
            Humanoid.Health = Humanoid.Health + (Damage*Lifesteal)
        end
        if FlamingBullet then
            local Debuff = TargetHumanoid.Parent:FindFirstChild("IgniteScript") or script.IgniteScript:Clone()
            Debuff.creator.Value = Player
            Debuff.Disabled = false
            Debuff.Parent = TargetHumanoid.Parent
        end
    end
end)

Youll notice ive modified it myself or attempted at it, I added TargetAddon to both of the scripts but that doesnt do anything.

Thank you for your time, hopefully some one may figure this out.

****Allow me to add the fact that the accessory is made of parts, not a mesh. The gun script works with accessories that are made of meshes but not ones that are made of parts.****

2 answers

Log in to vote
0
Answered by 7 years ago

Here's a piece of script from a gun i've been working on. You could try something like this for the hit detection:

 if hit then
                local humanoid =  hit.Parent:FindFirstChild("Humanoid")
                if humanoid then
                    if hit.Name == "Head" then
                        --//Double damage on headshots
                        humanoid:TakeDamage(dmg.Value*2)
                    else
                        --//Normal Damage on body shots
                        humanoid:TakeDamage(dmg.Value)
                    end
                end
            end

No matter where it hits on the player/NPC, it searches for a humanoid from the parent of the part you hit, and damages the humanoid

0
But its hitting a part inside an accessory. So the parent of the part is the accessory. GottaHaveAFunTime 218 — 7y
0
Plus isnt that what my script is already doing?? GottaHaveAFunTime 218 — 7y
0
Try making it check if hit.Parent is an accessory, if it is then it searches the parent of the accessory im_Draco 3 — 7y
0
It works when the accessory is made of meshes but when its made of parts it doesnt work. GottaHaveAFunTime 218 — 7y
Ad
Log in to vote
0
Answered by
UgOsMiLy 1074 Moderation Voter
7 years ago

iiDrac0's method would only partially work, because it searches for a humanoid in the parent of the part it hit. If it hit the Handle of an accessory, the parent would be the accessory, not the charater that the humanoid is found in.

The way that I use is to check the players from the Players service, look at their character, and see which character the brick is a descendant of (or, which character is an ancestor of the part that was hit; same thing.)

local TargetHumanoid = nil
for i,v in pairs(game.Players:GetPlayers())do
    if v and v.Character and v.Character:IsAncestorOf(Hit)then
        TargetHumanoid = v.Character:FindFirstChild("Humanoid")
        break
    end
end
0
It works with normal accesories thought, ones that are meshes. GottaHaveAFunTime 218 — 7y

Answer this question