So i'm making a simulator game on roblox, and you gather fun and sell it to get money or collect coins to get money, but I dont know how to Data Store. My script for Money and Fun is in leaderstats, My script which is in ServerScriptService is here:
codeblock local DS = game:GetService("DataStoreService"):GetDataStore("SaveData") game.Players.PlayerAdded:Connect(function(plr) wait() local plrkey = "id_"..plr.userid local save1 = plr.leaderstats.Fun local save2 = plr.leaderstats.Money local GetSaved = DS:GetAsync(plrkey) if GetSaved then save1.Value = GetSaved[1] save2.Value = GetSaved[2] else local NumberForSaving = {save1.Value, save2.Value} DS:GetAsync(plrkey, NumberForSaving) end end) game.Players.PlayerRemoving:Connect(function(plr) DS:SetAsync("id_"..plr.userid, {plr.leaderstats.Fun.Value, plr.leaderstats.Money.Value}) end)
I've tried it multiple times and it does nothing.
This might work:
local plrkey local save1 local save2 local DS = game:GetService("DataStoreService"):GetDataStore("SaveData") game.Players.PlayerAdded:Connect(function(plr) wait() plrkey = "id_"..plr.UserId save1 = plr.leaderstats.Fun save2 = plr.leaderstats.Money local GetSaved = pcall(function() DS:GetAsync(plrkey) end) if GetSaved then save1.Value = GetSaved[1] save2.Value = GetSaved[2] end end) game.Players.PlayerRemoving:Connect(function(plr) NumberForSaving = {save1.Value, save2.Value} local Saved = pcall(function() DS:SetAsync(plrkey,NumberForSaving) end) end)