#Trying to make coins stack up

40 messages · Page 1 of 1 (latest)

slim copper
#

trying to make coins stack up to an amount so i dont get infinite amount of coins

scritpt:

f = 1
while f<3 do
    
    
    local coin = game.ServerStorage.baseCoin:Clone()
    coin.Parent = game.workspace
    coin.Name = "Coin"
    print(f)
        coin.Touched:Connect(function(hit)
            local humanoid = hit.Parent:FindFirstChild("Humanoid")
                          
            if humanoid then
                local player = game.Players:GetPlayerFromCharacter(hit.Parent)
                    player.leaderstats.Coins.Value = player.leaderstats.Coins.Value +1    
                game.Workspace:WaitForChild("Coin"):Destroy()
                f = f - 1    
            
            end
            
        end)
    
    wait(5)
    f = f + 1
end
slim copper
#

its a loop that spawns coins every 5sec, when you touch them you collect them ( you destroy them and get 1 coin on the leaderstats)

#

and am trying to make it so that the coins stack up to 2 coins

coral ibex
slim copper
#

wym?

coral ibex
#

local f = 1

slim copper
#

oh

#

its kinda like i in for loops

coral ibex
#

first of all

#

use x += y instead of x = x + y

coral ibex
#

I dont get it

slim copper
#

like i want the maximum amount of coins that can spawn to be 2

coral ibex
#

ah

small lavaBOT
#

studio** You are now Level 9! **studio

coral ibex
#

and whats the problem you are facing?

slim copper
#

i get infinite yield if i dont do local f = 1 but if i do that then the loop runs how its supposed to the first time ( only 2 coins spawn) but if you collect the 2 coins next time the loop runs it will give me an inf amount of coins that spawn

small lavaBOT
#

studio** You are now Level 1! **studio

coral ibex
#

ngl but this script is kind of shit

#

wait

slim copper
#

yea am still new and am trying to learn thats why

coral ibex
coral ibex
# slim copper yea am still new and am trying to learn thats why
local f = 1
local debounce = false
while task.wait(5) do
    if f < 3 then
        print(f)
        local coin = game.ServerStorage.baseCoin:Clone()
        coin.Parent = game.workspace
        coin.Name = "Coin"
        coin.Touched:Connect(function(hit)
            if debounce then return end
            local player = game.Players:GetPlayerFromCharacter(hit.Parent)
            if player then
                debounce = true
                player.leaderstats.Coins.Value += 1    
                coin:Destroy()
                f -= 1
                task.wait(1)
                debounce = false
            end
        end)
        f += 1
    end
end

this isn't a very good script, just made it in a hurry in discord check if it works

slim copper
#

thx a lot

#

it works

#

if you dont mind can you explain why my code wasnt working?

coral ibex
#

so the ```lua
while f<3 do

#

this works until 3 is more than f

#

and if f is more than 3 or equals to

#

then it stops working

#

and after that it just stops

slim copper
#

oh so i had to add an if instead of while?

coral ibex
#

it goes forever

#

every 5 seconds

slim copper
#

oh okay

#

thx a lot