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

Data doesn't save when I leave too fast?

this is a code redeeming script, if I leave right after I redeemed it wouldn't save as I already redeemed it

local data = game:GetService("DataStoreService"):GetDataStore("codes2")
local ss = game:GetService("ServerStorage")
local activated = {}
game.Players.PlayerAdded:Connect(function(player)
	local key = "codes-"..player.userId
	local folder = Instance.new("Folder",player)
	folder.Name = "Codes"
	local save = data:GetAsync(key)
	if save then
	for i = 1, #save do
		local temp = Instance.new("BoolValue",folder)
		temp.Name = save[1]
		end
	end
end)
local ss = game:GetService("ServerStorage")
game.ReplicatedStorage.Remote.Redeem.OnServerEvent:Connect(function(player,code)
    local reward = player:FindFirstChild("leaderstats")
    local r = reward.Gold
    local key = "codes-"..player.userId
player.PlayerGui:WaitForChild("MainGui"):WaitForChild("Code")

    if code == "Release"  then
    if player.Codes:FindFirstChild(code) == nil then
    local bool = Instance.new("BoolValue")
    bool.Parent = player:FindFirstChild("Codes")
    bool.Name = code
    r.Value = r.Value + 300
player.PlayerGui:FindFirstChild("MainGui").Code.TextBox.Text = "Redeemed!"
wait(2)
player.PlayerGui:FindFirstChild("MainGui").Code.TextBox.Text = ""
	else
	player.PlayerGui:FindFirstChild("MainGui").Code.TextBox.Text = "Already Redeemed!"
	wait(1)
	player.PlayerGui:FindFirstChild("MainGui").Code.TextBox.Text = ""
end
end

	for i,v in pairs(player.Codes:GetChildren()) do
		if v:isA("BoolValue") then
			table.insert(activated, v.Name)
			data:SetAsync(key,activated)
			wait(60)
		end
	end
end)

game.Players.PlayerRemoving:Connect(function(plr)
	data:SetAsync("codes-"..plr.userId, activated)
end)

1 answer

alright here is the problem, Roblox datastore doesn't save arrays or objects.. it only saves strings(piece of text), and numbers.. if you'd like to easily save arrays and objects, i recommend turning them into JSON, which is represented as strings in Roblox, and therefore can be saved to Datastore.. The methods to encode and decode arrays and objects into JSON strings are JSONEncode and JSONDecode of HTTPService. GOOD luck

2 comments

0

the DataStore method works fine unless I leave the game right after I join

0

u sure? as far as I know, activated can't be saved in datastore as long as its not a string or number

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