Print Statement Prints multiple times?

This function will print “Damage” lots and lots of times for every time I want it to print, any explanation

local function Click()
	for i, world in pairs(game.Workspace:WaitForChild("Coins"):GetChildren()) do
		for i, coin in pairs(world:GetChildren()) do
			if coin:IsA("Model") then
				coin.Click.ClickDetector.MouseClick:Connect(function(plr)
					if #plr.EquippedPets:GetChildren() > 0 then
						wait(0.05)

						rs.Events.AttackCoin:FireClient(plr, coin)
						rs.Events.AttackCoinServer:Fire(plr, coin)

						while (coin ~= nil and wait(1) and plr) do
							print("Damage") --Printing lots of times
							
							if coin:FindFirstChild("Health") then
								if coin.Health.Value > 0 then
									for i, pet in pairs(plr.EquippedPets:GetChildren()) do
										coin.Health.Value -= pet.Power.Value
									end
								else
									coin:Destroy()
									break
								end
							end
						end
					end
				end)
			end
		end
	end
end

Ok, your print statement is inside of a while statement.
This is one reason it could be firing over and over.
The second possible reason is that you are calling your Click function by checking if the mouse is down, and not firing it when click happens.

how would I fix the while statement problem then?

Does anyone know how to make it not print multiple times or how I can use the print outside of the loop while still waiting 1 second?

I think the while statement probably isn’t it.
Show me how Click is being called.

You also want to check if its being printed every 1 second, or multiple ones being printed every second. That can tell us which one it is.

Its multiple ones printing all in 1 exact moment

local findCoin = require(script.FindCoin)

local rs = game:GetService("ReplicatedStorage")
local coins = rs.Coins

local coinsPerWorld = 75

local function Click()
	for i, world in pairs(game.Workspace:WaitForChild("Coins"):GetChildren()) do
		for i, coin in pairs(world:GetChildren()) do
			if coin:IsA("Model") then
				coin.Click.ClickDetector.MouseClick:Connect(function(plr)
					if #plr.EquippedPets:GetChildren() > 0 then
						wait(0.05)

						rs.Events.AttackCoin:FireClient(plr, coin)
						rs.Events.AttackCoinServer:Fire(plr, coin)

						while (coin ~= nil and wait(1) and plr) do
							print("Damage") --Printing lots of times
							
							if coin:FindFirstChild("Health") then
								if coin.Health.Value > 0 then
									for i, pet in pairs(plr.EquippedPets:GetChildren()) do
										coin.Health.Value -= pet.Power.Value
									end
								else
									coin:Destroy()
									break
								end
							end
						end
					end
				end)
			end
		end
	end
end

while wait() do
	for i, world in pairs(game.Workspace:WaitForChild("Coins"):GetChildren()) do
		if #world:GetChildren() < coinsPerWorld then		
			local coinToSpawn = findCoin.GetRandom(coins:GetChildren()):Clone()
			coinToSpawn.Parent = world
			coinToSpawn:SetPrimaryPartCFrame(CFrame.new(math.random(0, 100) - 50, (coinToSpawn.PrimaryPart.Size.Y/2), math.random(0, 100) - 50))
			
			local coinHealth = Instance.new("IntValue", coinToSpawn)
			coinHealth.Value = world:FindFirstChild("HealthValues"):FindFirstChild(coinToSpawn.Name).Value
			coinHealth.Name = "Health"
			
			local maxCoinHealth = Instance.new("IntValue", coinToSpawn)
			maxCoinHealth.Value = world:FindFirstChild("HealthValues"):FindFirstChild(coinToSpawn.Name).Value
			maxCoinHealth.Name = "MaxHealth"
			
			Click()
			
			wait(0.25)
		end
	end
end

Does anyone know why it is printing mutiple times?

That means you are calling it too often, and you need to check out how it is being called.

I dunno beyond that, seems weird.

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