Gun doing damage to everyone in the game

I have this ServerScript that controls the damage the player gives to others when they shoot and their shot hits another player. The problem is when that one player takes damage, for some reason everyone else in the game also takes damage except for the player that did the shot and I don’t know why.

ServerScript:

local replicatedStorage = game:GetService("ReplicatedStorage")
local workspace = game:GetService("Workspace")
local tweenService = game:GetService("TweenService")
local players = game:GetService("Players")

local armorRE = replicatedStorage:WaitForChild("armorUpdate")

local function damagePlayer(humanoid, damage)
	local armor = humanoid:GetAttribute("armor") 

	local armorDamage = math.min(damage, armor)
	local healthDamage = damage - armorDamage

	if armorDamage > 0 then
		humanoid:SetAttribute("armor", armor - armorDamage)
	end

	if healthDamage > 0 then
		humanoid:TakeDamage(damage)
	end
end

replicatedStorage.ToolRemoteEvents.Rev.OnServerEvent:Connect(function(player, mouse, tool)
	local character = player.Character
	local reload = character:WaitForChild("BodyEffects"):WaitForChild("Reload")
	local barrel = tool.Barrel
	local handle = tool.Handle
	local beamDisappearTime = 0.45
	
	if tool.Ammo.Value >= 1 then
		local GUI = player:WaitForChild("PlayerGui"):WaitForChild("Weapons"):WaitForChild("Revolver")
		local GoingToShootBullet = GUI:WaitForChild("MainFrame"):FindFirstChild("BulletImage")

		if GoingToShootBullet then
			GoingToShootBullet.ImageTransparency = 0.6
			GoingToShootBullet.Name = "UsedBullet"

			if not reload.Value then
				reload.Value = true

				barrel.ShootUI.Shoot.ImageTransparency = 0.6
				barrel.ShootUI.Shoot.Visible = true
				handle.ShootLight.Enabled = true

				local Shoot = Instance.new("Sound")	
				Shoot.SoundId = "rbxassetid://1583819337"
				Shoot.Parent = handle
				Shoot:Play()

				local StartPoint = Instance.new("Part")
				StartPoint.Transparency = 1
				StartPoint.Parent = workspace
				StartPoint.Position = barrel.Position
				StartPoint.Size = Vector3.new(0.5, 0.5, 0.5)
				StartPoint.Anchored = true
				StartPoint.CanCollide = false

				local EndPoint = Instance.new("Part")
				EndPoint.Transparency = 1
				EndPoint.Parent = workspace
				EndPoint.Position = mouse
				EndPoint.Size = Vector3.new(0.5, 0.5, 0.5)
				EndPoint.Anchored = true
				EndPoint.CanCollide = false	

				local attachment1 = Instance.new("Attachment")
				local attachment2 = Instance.new("Attachment")
				attachment1.Parent = StartPoint
				attachment2.Parent = EndPoint

				local beam = Instance.new("Beam")
				beam.Color = ColorSequence.new({
					ColorSequenceKeypoint.new(0, Color3.fromRGB(255, 170, 0)),
					ColorSequenceKeypoint.new(1, Color3.fromRGB(255, 255, 127)),
				})
				beam.LightEmission = 1
				beam.Attachment1 = attachment2
				beam.Attachment0 = attachment1
				beam.Width0 = 0.002
				beam.Width1 = 0.09
				beam.FaceCamera = true
				beam.Transparency = NumberSequence.new(0)
				beam.Parent = StartPoint

				local rayDirection = mouse
				local rayDesti = rayDirection - tool.Barrel.Position

				local ignorelist = {character, tool}
				local ray = Ray.new(tool.Barrel.Position, rayDesti)

				local position = workspace:FindPartOnRayWithIgnoreList(ray, ignorelist)

				if position then 
					for i, v in pairs(players:GetPlayers()) do
						if v.Character and v ~= player then
							local humanoid = v.Character:FindFirstChild("Humanoid")	
							local distance = (mouse - tool.Barrel.Position).Magnitude
							
							if not v.Character:WaitForChild("BodyEffects"):WaitForChild("Ragdolled").Value then
								if distance <= 5 then
									damagePlayer(humanoid, 28)
								else
									damagePlayer(humanoid, 20)
								end
							end	
						end
					end
				end
				
				task.wait(0.1)
				reload.Value = false
				barrel.ShootUI.Shoot.Visible = false
				handle.ShootLight.Enabled = false
				
				for i = 0, 1, wait() / beamDisappearTime do
					beam.Transparency = NumberSequence.new(0 + (1 - 0) * i)
					task.wait()
				end
				beam.Transparency = NumberSequence.new(1)
				
				beam:Destroy()
				attachment1:Destroy()
				attachment2:Destroy()
				StartPoint:Destroy()
				EndPoint:Destroy()

				Shoot.Ended:Wait()
				Shoot:Destroy()
			end
		else
			if character:WaitForChild("BodyEffects"):WaitForChild('Ragdolled').Value then return end
			
			handle.NoAmmo:Play()
		end
	end
end)

Its because you loop through the players with no check if the player in the loop is actually the one being hit, you just check if a player is within a radius and then deal extra damage resulting in 20 damage to everyone else

Edit: issue is in this for i loop

					for i, v in pairs(players:GetPlayers()) do
						if v.Character and v ~= player then
							local humanoid = v.Character:FindFirstChild("Humanoid")	
							local distance = (mouse - tool.Barrel.Position).Magnitude
							
							if not v.Character:WaitForChild("BodyEffects"):WaitForChild("Ragdolled").Value then
								if distance <= 5 then
									damagePlayer(humanoid, 28)
								else
									damagePlayer(humanoid, 20)
								end
							end	
						end
					end

u looped through every player, thats the problem oof

the issue is you are using the same distance as it checks each player in the loop. so its not actually checking each players distance

You are looping through all the players in the game and checking if they are not ragdolled, so every player in the game that isn’t in the state of ragdoll will get damage, except the player who shot the bullet.

How could I have it check if the player in the loop is the one being hit? I’m not too knowledgeable on how to go about this.

You should move onto the more modern raycasting instead of using Ray.new()
This roblox documentation should help you a lot

Edit: you can use Ray.new() as well but I recommend this, you can retrieve the instance being hit, get the position of the contact and all that easily

1 Like

Instead of using a for loop that goes through every player, shouldn’t you just get the parent of the part that the ray hit then see if the parent of that part has a humanoid in it them damage THAT humanoid?

I don’t really know how to use raycasts so… correct me if I’m wrong.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.