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

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

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

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.

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