Hello, I am working on a game that uses a data store script to save the amount of currency you have but for some reason it does not work.
local DataStore = game:GetService("DataStoreService") local ds = DataStore:GetDataStore("ToadsSaver") game.Players.PlayerAdded:connect(function(player) local leader = Instance.new("Folder",player) leader.Name = "leaderstats" local Toads = Instance.new("IntValue",leader) Toads.Name = "Toads" Toads.Value = ds:GetAsync(player.UserId) or 0 ds:SetAsync(player.UserId, Toads.Value) Toads.Changed:connect(function() ds:SetAsync(player.UserId, Toads.Value end) end) game.Players.PlayerRemoving:connect(function(player) ds.SetAsync(player.UserId, player.leaderstats.Toads.Value) end)
This code will work!
--[[Savin' Dem Stats --]] game.Players.PlayerRemoving:connect(function(player) local datastore = game:GetService("DataStoreService"):GetDataStore(player.Name.."Stats") local statstorage = player:FindFirstChild("leaderstats"):GetChildren() for i = 1, #statstorage do datastore:SetAsync(statstorage[i].Name, statstorage[i].Value) print("saved data number "..i) end print("Stats successfully saved") end) --[[ Loadin' Dem Stats --]] game.Players.PlayerAdded:connect(function(player) local datastore = game:GetService("DataStoreService"):GetDataStore(player.Name.."Stats") player:WaitForChild("leaderstats") wait(1) local stats = player:FindFirstChild("leaderstats"):GetChildren() for i = 1, #stats do stats[i].Value = datastore:GetAsync(stats[i].Name) print("stat numba "..i.." has been found") end end)
Marked as Duplicate by Troidit, cabbler, Leamir, and Gey4Jesus69
This question has been asked before, and already has an answer. If those answers do not fully address your question, then please ask a new question here.
Why was this question closed?