Hello! I would like to know how I can adapt my code so that when 2 minutes pass, 20 cash is earned, how can I make that possible? could someone help me please?
Example, time in the game 2 minutes, after those 2 minutes +20 cash is added to all players who are connected (online)
local DataStoreService = game:GetService("DataStoreService")
local myDataStore = DataStoreService:GetDataStore("myDataStore")
game.Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local cash = Instance.new("IntValue")
cash.Name = "Cash"
cash.Parent = leaderstats
local data
local success, errormessage = pcall(function()
data = myDataStore:GetAsync(player.UserId.."-cash")
end)
if success then
cash.Value = data
else
print("There was an error with getting your data.")
wait(errormessage)
end
end)
game.Players.PlayerRemoving:Connect(function(player)
local success, errormessage = pcall(function()
myDataStore:SetAsync(player.UserId.."-cash", player.leaderstats.Cash.Value)
end)
if success then
print("Player data successfully saved!")
else
print("There was an error when saving data.")
warn(errormessage)
end
end)
At the bottom of the script, run this code. For texting purposes, change the wait to something like wait(5)
while wait(120) do --every 120 seconds
for i, player in pairs(game.Players:GetChildren()) do --loop through players
player:WaitForChild("leaderstats"):WaitForChild("Cash").Value += 20
end
end
local DataStoreService = game:GetService("DataStoreService")
local myDataStore = DataStoreService:GetDataStore("myDataStore")
game.Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local cash = Instance.new("IntValue")
cash.Name = "Cash"
cash.Parent = leaderstats
local data
local success, errormessage = pcall(function()
data = myDataStore:GetAsync(player.UserId.."-cash")
end)
if success then
cash.Value = data
else
print("There was an error with getting your data.")
wait(errormessage)
end
-- Earn 20$ every 2 mins
while wait(120) do --every 120 seconds
for i, player in pairs(game.Players:GetChildren()) do --loop through players
player:WaitForChild("leaderstats"):WaitForChild("Cash").Value += 20
end
end
-- End code
end)
game.Players.PlayerRemoving:Connect(function(player)
local success, errormessage = pcall(function()
myDataStore:SetAsync(player.UserId.."-cash", player.leaderstats.Cash.Value)
end)
if success then
print("Player data successfully saved!")
else
print("There was an error when saving data.")
warn(errormessage)
end
end)
The code above will make a new connection every time someone joins. You want only one loop at the very bottom of the script. Like this:
local DataStoreService = game:GetService("DataStoreService")
local myDataStore = DataStoreService:GetDataStore("myDataStore")
game.Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local cash = Instance.new("IntValue")
cash.Name = "Cash"
cash.Parent = leaderstats
local data
local success, errormessage = pcall(function()
data = myDataStore:GetAsync(player.UserId.."-cash")
end)
if success then
cash.Value = data
else
print("There was an error with getting your data.")
wait(errormessage)
end
end)
game.Players.PlayerRemoving:Connect(function(player)
local success, errormessage = pcall(function()
myDataStore:SetAsync(player.UserId.."-cash", player.leaderstats.Cash.Value)
end)
if success then
print("Player data successfully saved!")
else
print("There was an error when saving data.")
warn(errormessage)
end
end)
-- Earn 20$ every 2 mins
while wait(120) do --every 120 seconds
for i, player in pairs(game.Players:GetChildren()) do --loop through players
player:WaitForChild("leaderstats"):WaitForChild("Cash").Value += 20
end
end
-- End code
Please use spawn() or coroutine.resume(coroutine.create(function() before the while loop. Not doing so will make so that the rest of the script stops running.