local module = {}
while task.wait(1) do
for _, player in game.Players:GetPlayers() do
player:FindFirstChild("leaderstats"):FindFirstChild("Coins").Value += player.leaderstats:FindFirstChild("Cows").Value * player:FindFirstChild("PlayerStats"):FindFirstChild("MilkingRate").Value * player.PlayerStats:FindFirstChild("CoinMultiplier").Value
end
end
return module
This works fine in studio, but is this the best way of going about this? I feel like this is very error prone with players loading in and out
First off, there is no reason to put this in a module script. I believe it will error if you require() this module script because the while loop yields causing nothing to return.
This is how I would format your script. Please don’t put everything in one line lol. Also remember, there is no reason to use :FindFirstChild() if you’re not actually checking if it’s there, all this is doing is wasting memory. Just use brackets instead since there should be no reason “MilkingRate” or “CoinMultiplier” is not inside player stats.
while task.wait(1) do
for _, player in ipairs(game.Players:GetPlayers()) do
local leaderstats = player:FindFirstChild("leaderstats")
local playerStats = player:FindFirstChild("PlayerStats")
if leaderstats and playerStats then
local cows = leaderstats["Cows"].Value
local milkingRate = playerStats["MilkingRate"].Value
local coinMultiplier = playerStats["CoinMultiplier"].Value
leaderstats["Coins"].Value += cows * milkingRate * coinMultiplier
end
end
end
This is pretty much the best way to give player coins automatically.