Coins per minute

Hey there.

How do I add a part to this script that will give me 5 coins every minute?

Here’s the script :

local DS = game:GetService("DataStoreService")
local moneyStore = DS:GetDataStore("Coins")
local remote = game:GetService("ReplicatedStorage").Remotes.GiveCurrency

game.Players.PlayerAdded:Connect(function(player)

	local moneyValue
	local success, err = pcall(function()
		moneyValue = moneyStore:GetAsync("Player_"..player.UserId)
	end)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player

	local money = Instance.new("IntValue")
	money.Name = "Coins"
	money.Parent = leaderstats	
	
	if success then
		money.Value = moneyValue
	else
		print("Failed to load data")
	end
end)

local function save(player)
	local success, err = pcall(function()
		moneyStore:SetAsync("Player_"..player.UserId, player.leaderstats.Coins.Value)
	end)
	if success then
		print("Saved data")
	else
		print("Failed to load data")
	end
end

local function autosave()
	while wait(5) do
		for i, player in pairs(game:GetService("Players"):GetPlayers()) do
			save(player)
		end
	end
end

remote.OnServerEvent:Connect(function(player, amount)
	player.leaderstats.Coins.Value += amount
end)

spawn(autosave)
1 Like

Just insert a while loop to give the Player Coins every 60 seconds inside your PlayerAdded event

game.Players.PlayerAdded:Connect(function(player)

	local moneyValue
	local success, err = pcall(function()
		moneyValue = moneyStore:GetAsync("Player_"..player.UserId)
	end)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player

	local money = Instance.new("IntValue")
	money.Name = "Coins"
	money.Parent = leaderstats	
	
	if success then
		money.Value = moneyValue
	else
		print("Failed to load data")
	end

    while Player do
        wait(60)
        money.Value += 5
    end
end)

(No don’t replace this with your entire script)

Where do I add this? if possible, could you send me the whole script, please?

Just replace that with your PlayerAdded event, it should work just fine

(You should know what a PlayerAdded event is right?)

uh, not really, I’m not the greatest at scripting…

Oh wait, I see it, let me see if this right.

local DS = game:GetService("DataStoreService")
local moneyStore = DS:GetDataStore("Coins")
local remote = game:GetService("ReplicatedStorage").Remotes.GiveCurrency

game.Players.PlayerAdded:Connect(function(player)

	local moneyValue
	local success, err = pcall(function()
		moneyValue = moneyStore:GetAsync("Player_"..player.UserId)
	end)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player

	local money = Instance.new("IntValue")
	money.Name = "Coins"
	money.Parent = leaderstats	
	
	if success then
		money.Value = moneyValue
	else
		print("Failed to load data")
	end

    while Player do
        wait(60)
        money.Value += 5
    end
end)

remote.OnServerEvent:Connect(function(player, amount)
	player.leaderstats.Coins.Value += amount
end)

spawn(autosave)

Is this right?

Inhale

Not to be rude or anything but I do recommend looking on how you can learn to script, search Youtube videos if you have to :slightly_smiling_face:

local DS = game:GetService("DataStoreService")
local moneyStore = DS:GetDataStore("Coins")
local remote = game:GetService("ReplicatedStorage").Remotes.GiveCurrency

game.Players.PlayerAdded:Connect(function(player)

	local moneyValue
	local success, err = pcall(function()
		moneyValue = moneyStore:GetAsync("Player_"..player.UserId)
	end)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player

	local money = Instance.new("IntValue")
	money.Name = "Coins"
	money.Parent = leaderstats	
	
	if success then
		money.Value = moneyValue
	else
		print("Failed to load data")
	end

    while Player do
        wait(60)
        money.Value += 5
    end
end)

local function save(player)
	local success, err = pcall(function()
		moneyStore:SetAsync("Player_"..player.UserId, player.leaderstats.Coins.Value)
	end)
	if success then
		print("Saved data")
	else
		print("Failed to load data")
	end
end

local function autosave()
	while wait(5) do
		for i, player in pairs(game:GetService("Players"):GetPlayers()) do
			save(player)
		end
	end
end

remote.OnServerEvent:Connect(function(player, amount)
	player.leaderstats.Coins.Value += amount
end)

spawn(autosave)
1 Like

I don’t want to be rude but then how did you make that script? (Or was it a YouTube video that you copy and pasted the tutorials source code from…?)

Wouldn’t you actually want to use tick instead of wait because I’ve heard using wait is unreliable. You would save the tick and then if the current tick - saved tick >= 300 or something like that.

I used a Youtube Video to help me, and I couldn’t do it myself.

It’s only unreliable if you use it so much in such a short span of time, which is why we use RunService’s Events such as RenderStepped, Stepped, Heartbeat, and etc

I don’t think it would matter anyways if we’re only giving Coins to the Player every close to 60 seconds

1 Like

It works, thank you so much! Appreciate it alot.