#Please help me with this code, it should spawn a coin every five seconds

1 messages · Page 1 of 1 (latest)

naive juniper
#
local RPS = game:GetService("ReplicatedStorage")
local coinTemplate = RPS.Coin
local player = game:GetService("Players").LocalPlayer



local function spawnCoin()
    local char = player.Character
    local root = char:FindFirstChild("HumanoidRootPart")
    if not root then return end
    local Coin = coinTemplate:Clone()
    Coin.Parent = game.Workspace
    Coin.Position = root.Position + Vector3.new(math.random(-10,10), 5, math.random(-10,10))
    Coin.Anchored = false
    
    Coin.Touched:Connect(function(hit)
        if hit:IsDescendantOf(char) then
            local stats = player:FindFirstChild("leaderstats")
            if stats and stats:FindFirstChild("Coin") then
                stats.Coin.Value += 1
                Coin:Destroy()
            end
        end
    end)
end
task.spawn(function()
    while true do
        task.wait(5)
        print("Called")
        spawnCoin()
    end

end)

tropic knot
#

What exactly is it failing to do? Is it not spawning the coin, is it not activating the loop, like what is it failing?

tropic knot
#

maybe try removing local from spawnCoin?

naive juniper
#

Ok I'll try soon

naive juniper
tropic knot
#

Okay

#

Lets try to find where specifically it stops

#

@naive juniper does it print "Called"?

#

if so, if you add a print at the start of spawnCoin, does it print that?

naive juniper
tropic knot
#

okay cool

#

Just curious, what was the fix?

naive juniper
#

Because replicated storage cannot load scripts