Gain Coins Every Second for Every Player

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

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

I believe you can just add multiple checks such as if the player and leader stats exist and just stuff of the sort.

If i do that it wont be super inefficient? I dont want to affect the games performance idk if this is too little of a function to affect anything tho

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

This is pretty much the best way to give player coins automatically.