Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

I don't know how to data store, my script won't work. My variables are Fun and Money. Can you help?

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.

1 comment

0

please add everything in a codeblock. Makes it more organized :)

1 answer

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)

1 comment

0

it did not work. I'm putting it under the right thing right? I'm named the script SaveData and I put it under ServerScriptService?

This discussion is preserved as it was. New answers, comments, and votes are closed.