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.
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