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

How do i make this hitbox and damage script not damage the player, but the enemy player?

Asked by
Jirozu 71
7 years ago

I made this hit box and damage script, and once it executes it damages the enemy player. But sometimes when i execute it, it damages the player, and not the enemy player, any help?

--Hitbox to do damage
        local Hitbox = Instance.new("Part", game.Workspace)
        Hitbox.Anchored = false
        Hitbox.CanCollide = false
        Hitbox.Transparency = 1
        Hitbox.BrickColor = BrickColor.new("Bright red")
        Hitbox.Size = Vector3.new(1.3, 1.3, 1.3)

        local Weld = Instance.new("Weld", Hitbox)
        Weld.Part0 = Hitbox
        Weld.Part1 = Character.LeftHand
        Weld.C0 = Weld.C0 * CFrame.new(0, 0, 0)

        --Damage
        Hitbox.Touched:connect(function(p)
            if script.CanDamage.Value == true then
                script.CanDamage.Value = false
                p.Parent.Humanoid:TakeDamage(Player.Data.Strength.Value+5)
                print("Hit")
                --Effect
                for i = 1,5 do
                    local Effect2 = game.ReplicatedStorage.MeshEffects.SpinningCircle:Clone()
                    Effect2.BrickColor = BrickColor.new("Medium stone grey")
                    Effect2.Size = Vector3.new(3, 3, 3)
                    Effect2.Parent = game.Workspace
                    Effect2.CFrame = p.Parent.LowerTorso.CFrame * CFrame.Angles(0, math.random(-90, 90), 0)
                end
            end
        end)

1 answer

Log in to vote
0
Answered by 7 years ago

There is nothing checking to see what the hit player(p) is. If the Hitbox touches the Player, then it will deal damage accordingly.

Try adding...

Player = game.Players:GetPlayerFromCharacter(p.Parent)
if Player == --[===[put the player you don't want to get hurt here]===] then
--code

If you're wondering why it doesn't hurt the enemy player when this happens, it's because you set the CanDamage.Value to false while executing the damage code. Because the Player is hit first, the enemy player cannot take damage until the value is changed to true.

Ad

Answer this question