I want to make a random coin spawner but the problem is when I do this every time I touch a coin nothing happens. I’ve tried to use a touched event in a while true do statement but I don’t like that method and I’m not sure how else to approach random coin spawners. Is there any other ways I can make random coin spawners or fix this issue?
coin spawner script:
local sp = workspace.SpawnParts:GetChildren()
local rs = game:GetService("ReplicatedStorage")
local cs = game:GetService("CollectionService")
local coin = rs.Coin
local debounce = false
function makeCoin()
local randomnumber = math.random(1,#sp)
local chosenpart = sp[randomnumber]
local newcoin = coin:Clone()
newcoin.Parent = workspace.CoinFolder
newcoin.CFrame = chosenpart.CFrame
end
while task.wait(2) do
spawn(makeCoin)
end
for i,v in pairs(cs:GetTagged("Coin")) do
v.Touched:Connect(function(hit)
local player = game.Players:GetPlayerFromCharacter(hit.Parent)
if player then
v:Destroy()
player.leaderstats.Money.Value = player.leaderstats.Money.Value + 5
end
end)
end
Datastoring and leaderstats:
local DSS = game:GetService("DataStoreService")
local datastore = DSS:GetDataStore("DataStore")
game.Players.PlayerAdded:Connect(function(plr)
local folder = Instance.new("Folder", plr)
folder.Name = "leaderstats"
local money = Instance.new("IntValue", folder)
money.Name = "Money"
local data
local success, errormessage = pcall(function()
data = datastore:GetAsync(plr.UserId.."-money")
end)
if success then
money.Value = data
else
print("Error")
warn(errormessage)
end
end)
game.Players.PlayerRemoving:Connect(function(plr)
local success, errormessage = pcall(function()
datastore:SetAsync(plr.UserId.."-money", plr.leaderstats.Money.Value)
end)
if success then
print("Data Saving")
else
print("Error")
warn(errormessage)
end
end)
There are other scripts in the workspace but they are very irrelevant to the coin spawning script issue.
Put your loop after the other one. You should also be using a function to track when new “Coin” instances are added. It also is not needed to use CollectionService, because you’re handling the creation of a new coin, so you can just connect the function inside. I’ve kept CollectionService though, just incase you have another use case.
Code:
local rs = game:GetService("ReplicatedStorage")
local cs = game:GetService("CollectionService")
local coin = rs.Coin
local sp = workspace.SpawnParts:GetChildren()
local debounce = false
local function makeCoin()
local randomnumber = math.random(#sp)
local chosenpart = sp[randomnumber]
local newcoin = coin:Clone()
newcoin.CFrame = chosenpart.CFrame
newcoin.Parent = workspace.CoinFolder
end
local function initializeCoin(coin)
coin.Touched:Connect(function(hit)
local player = game.Players:GetPlayerFromCharacter(hit.Parent)
if player then
coin:Destroy()
player.leaderstats.Money.Value += 5
end
end)
end
cs:GetInstanceAddedSignal("Coin"):Connect(initializeCoin)
for i, v in cs:GetTagged("Coin") do
task.spawn(initializeCoin, v)
end
while task.wait(2) do
makeCoin()
end
local genRate = 1/2 --1 coin per 2 seconds
local function makeCoin()
local sp = workspace.SpawnParts:GetChildren()
local coin: BasePart = game.ReplicatedStorage.Coin:Clone()
coin.CFrame = sp[math.random(1, #sp)].CFrame
coin.Touched:Connect(function(hit)
local player = game.Players:GetPlayerFromCharacter(hit.Parent)
if not player then return end
player.leaderstats.Money.Value += 5
coin:Destroy()
end)
coin.Parent = workspace.CoinFolder
end
while task.wait(1/genRate) do makeCoin() end
Other than that, your datastore code looks like you followed a tutorial which is a completely normal thing to do when learning. However, that code can cause many issues down the line if it is put in production, more specifically because of not handling data errors and for instancing leaderstats values before setting them from the datastore. This isn’t related to the topic directly so take it as a friendly warning to have in mind. Also, there’re plenty datastore libraries out there for handling data errors, rate limits, and weird scenarios in the background, you don’t have to reinvent the wheel to ensure your player data is safe.
There’s no point in your pcall currently, all it does is printing the error but without handling it. So if the API request fails, the player will load with 0 coins instead of their normal amount, and when they leave if the next request succeeds the coins they collected within a single session(let’s say 25) will override their actual coins(let’s say 510) causing data loss.
Also if the error happens when they leave instead of when they join, their data will stay in its previous state before that game session, which means that they will most likely waste hours of gameplay to gain coins, and then when they rejoin see their coins reset back to the amount they had before they did that in-game grind/farming.