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)
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)
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)
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
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)
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.
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