Why does this damage more than once?

I have a gun system, and it for some reason, does the damage 42 different times. The remote runs once, and I don’t know why this loops 42 different times, how would I fix this?

if guntype == "Pistol" then
							--print(Ranges)
							--print(DamageStat)
							local shothit = false
								local origin = plr.Character.Head.Position +  plr.Character.Head.CFrame.LookVector * 5
								local position = pos2
								local direction = (position - origin).Unit*(Ranges * 10)
								local raycastParams = RaycastParams.new()
								raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
								raycastParams.FilterDescendantsInstances = {plr.Character:GetDescendants(),workspace.Effects:GetDescendants()}
								local raycastResult = workspace:Raycast(origin, direction,raycastParams)
								local intersection = raycastResult and raycastResult.Position or origin + direction
								local distance = (origin - intersection).Magnitude
								local partx = Instance.new("Part",workspace.Effects)
								--part:SetNetworkOwner(nil)
								local midpoint = (origin + pos2)/2 
								partx.Transparency = 0
								partx.Anchored = true 
								partx.CanCollide = false 
								partx.Size = Vector3.new(0.45, 0.45, 0.45)
								--if findgun[1] == "RPG-7" then
								--			local all = game.ServerStorage.Explosions:getChildren()
								--			task.spawn(function()
								--				task.wait()
								--				beamcde[plr.Name] = false
								--				shootsoundcd[plr.Name] = false
								--			end)
								--for i=1, #all do
								--	local vasx=all[i]:Clone()
								--	vasx.Parent = partx
								--			vasx:Emit(math.random(5,50))
								--	end
								--		findgun[2]:Destroy()
								--		end
								--	end)
								game.Debris:AddItem(partx,1.35)
								if raycastResult then
								partx.Position = raycastResult.Instance.Position
								else
									partx.Position =  origin + direction
							end
							if (partx.Position - damagetaker.HumanoidRootPart.Position).magnitude <= 2 then
								if shothit == true then return end
								if (damagetaker.Humanoid.Health - DamageStat) >= 0 then
									if shothit == true then return end 
									shothit = true
									damagetaker.Humanoid:TakeDamage(DamageStat)
								else
									if shothit == true then return end
									shothit = true
									damagetaker.Humanoid.Health = 0
									end
								end
								return end

Have you tried a debounce?

2 Likes

Oh yeah, I completely forgot to update this. Turns out I had the Range Stats and the Damage Stats swapped, Causing it do 420 damage. I’ll give you the solution for your attempt

1 Like