#Assets Despawning

1 messages · Page 1 of 1 (latest)

spark herald
#

Hi evil developpers, i was making a coin spawner system and encountered an annoying problem. I have spawners inside a folder, and coins are supposed to spawn in a folder while being prented to their respective spawner. But here's the problem: On joining, the spawners get destroyed after a few seconds, and the coins respawns idk where.

I looked everywhere, and i don't understand whats going on. Please help, i need ts to work to make my full game.

Heres the script:

local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Workspace = game:GetService("Workspace")
local DataStoreService = game:GetService("DataStoreService")
local coinStore = DataStoreService:GetDataStore("PlayerCoins")

local coinTemplate = ReplicatedStorage:WaitForChild("CoinTemplate")
local spawnerFolder = Workspace:WaitForChild("CoinSpawners")
local coinsFolder = Workspace:FindFirstChild("Coins") 

local respawn = 30


--id giver 
for i, spawner in ipairs(spawnerFolder:GetChildren()) do
    if spawner:IsA("BasePart") then
        spawner:SetAttribute("SpawnerID", "Spawner_" .. i)
    end
end

local function spawnCoin(spawner)
    local spawnerID = spawner:GetAttribute("SpawnerID")
    if not spawnerID then return end

    for _, child in ipairs(coinsFolder:GetChildren()) do
        if child:GetAttribute("SpawnerID") == spawnerID then
            return
        end
    end

    local coin = coinTemplate:Clone()
    coin.CFrame = spawner.CFrame * CFrame.new(0,1,0)
    coin.Anchored = true
    coin.CanCollide = false
    coin.Parent = coinsFolder
    
#
coin:SetAttribute("SpawnerID", spawnerID)

    local collected = false
    local connection
    connection = coin.Touched:Connect(function(hit)
        if collected then return end
        collected = true
        connection:Disconnect()

        local player = Players:GetPlayerFromCharacter(hit.Parent)
        if not player then return end

        local leaderstats = player:FindFirstChild("leaderstats")
        if not leaderstats then return end
        local coinsStat = leaderstats:FindFirstChild("Coins")
        if not coinsStat then return end

        coinsStat.Value += 1
        coin:Destroy()

        -- wait
        task.spawn(function()
            task.wait(respawn)
            spawnCoin(spawner)
        end)
    end)
end

for _, spawner in ipairs(spawnerFolder:GetChildren()) do
    if spawner:IsA("BasePart") then
        spawnCoin(spawner)
    end
end

-- data and leaderstas
Players.PlayerAdded:Connect(function(player)

    task.wait(0.3)--WAIT OR ELSE
    local leaderstats = player:FindFirstChild("leaderstats")


    local coinsStat = leaderstats:FindFirstChild("Coins")
    if not coinsStat then
        coinsStat = Instance.new("IntValue")
        coinsStat.Name = "Coins"
        coinsStat.Value = 0
        coinsStat.Parent = leaderstats
    end
    
    --load
    local success, data = pcall(function()
        return coinStore:GetAsync(player.UserId)
    end)
    if success and typeof(data) == "number" then
        coinsStat.Value = data
    end
end)

Players.PlayerRemoving:Connect(function(player)
    local leaderstats = player:FindFirstChild("leaderstats")
    if not leaderstats then return end
    local coinsStat = leaderstats:FindFirstChild("Coins")
    if not coinsStat then return end

    pcall(function()
        coinStore:SetAsync(player.UserId, coinsStat.Value)
    end)
end)
#

I genuinely need help

#

all of them despawn after a few seconds

#

fuck

spark herald
#

PLEASE

untold oracle
#

Are they anchored and can collide on

spark herald