Hey there!
I have recently watched and used AlvinBlox’s shop gui tutorial, but I ran into a problem. I can’t figure out how to get it to add x amount of cash to every players’ stats every x amount of minutes. Every time I try, either an error occurs, or it doesn’t save the data. (Because I didn’t add in the code right. I have Studio access to API Services on, so that’s not the issue.) I’m stumped.
Here’s the script that I’ve tried most recently:
local DataStore = game:GetService("DataStoreService"):GetDataStore("MyDataStore")
game.Players.PlayerAdded:Connect(function(plr)
local folder = Instance.new("Folder")
folder.Name = "leaderstats"
folder.Parent = plr
local cash = Instance.new("IntValue")
cash.Name = "Cash"
cash.Parent = folder
local data
local cashStored
local success, errorMessage = pcall(function(plr)
data = DataStore:GetAsync(plr.UserId.."tools")
cashStored = DataStore:GetAsync(plr.UserId.."cash")
end)
if cashStored ~= nil then
cash.Value = cashStored
else
cash.Value = 100
end
if data ~= nil then
for _, toolName in pairs(data) do
local tool = game.ReplicatedStorage.Tools:FindFirstChild(toolName)
if tool then
local NewTool = tool:Clone()
NewTool.Parent = plr.Backpack
local NewTool = tool:Clone()
NewTool.Parent = plr.StarterGear
end
end
end
while true do --I added in this
cash.Value = cashStored + 20
wait(5)
end
end)
game.Players.PlayerRemoving:Connect(function(plr)
local toolsTable = {}
for _, tool in pairs(plr.Backpack:GetChildren()) do
if game.ReplicatedStorage.Tools:FindFirstChild(tool.Name) then
table.insert(toolsTable, tool.Name)
end
end
local success, errorMessage = pcall(function()
DataStore:SetAsync(plr.UserId.."tools",toolsTable)
DataStore:SetAsync(plr.UserId.."cash",plr.leaderstats.Cash.Value)
end)
end)
game:BindToClose(function()
for _, plr in pairs(game.Players:GetPlayers()) do
local toolsTable = {}
for _, tool in pairs(plr.Backpack:GetChildren()) do
if game.ReplicatedStorage.Tools:FindFirstChild(tool.Name) then
table.insert(toolsTable, tool.Name)
end
end
local success, errorMessage = pcall(function()
DataStore:SetAsync(plr.UserId,toolsTable)
end)
end
end)
Any and all help is much appreciated! Thank you so much.