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

Datastore script saves one value but not the other?

So i've made a datastore script to save the player's data (tix and worlds), when i test the game it saves the "tix" value but now the "worlds" value, can someone help me fix this? I know i probably made a punctuation mistake or something

The script:

local DataStoreService = game:GetService("DataStoreService")

local myDataStore = DataStoreService:GetDataStore("myDataStore")

local playersLeft = 0

game.Players.PlayerAdded:Connect(function(player)
	
	playersLeft = playersLeft + 1
	
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
	
	local tix = Instance.new("IntValue")
	tix.Name = "Tix"
	tix.Value = 0
	tix.Parent = leaderstats
	
	local worlds = Instance.new("IntValue")
	worlds.Name = "Worlds"
	worlds.Value = 0
	worlds.Parent = leaderstats
	
	local tixData, worldsData
	local success, errormessage = pcall (function()
		tixData = myDataStore:GetAsync(player.UserId.."-tix")
		worldsData = myDataStore:GetAsync(player.UserId.."-worlds")
	end)
	
	if success then
		tix.Value = tixData
		worlds.Value = worldsData
		print("Player data loaded successfuly!")
	else
		print("There was an error whilst getting your data!")
		warn(errormessage)
	end
end)

local bindableEvent = Instance.new("BindableEvent")

game.Players.PlayerRemoving:Connect(function(player)
	
	local success, errormessage pcall(function()
		myDataStore:SetAsync(player.UserId.."-tix",player.leaderstats.Tix.Value)
		myDataStore:SetAsync(player.UserId.."-worlds",player.leaderstats.Worlds.Value)
	end)
	
	if success then
		print("Player data successfuly saved!")
		playersLeft = playersLeft - 1
		bindableEvent:Fire()
	else
		print("There was an error when saving data!")
		warn(errormessage)
	end
	
end)

game:BindToClose(function()
	while playersLeft > 0 do
		bindableEvent.Event:Wait()
	end
end)

So yeah can anybody help me?

1 answer

✓ Accepted answer

The datastore can only save ONE value, so you'll need to get em in a table, and save the table, encoding it into json formatted strings(because datastores only save int's, number's and strings) just like that:

local httpservice = game:GetService("HttpService")
local data = {tix.Value,worlds.Value}
local encodeData = httpservice:JSONEncode(data)
datastore:SetAsync(key,encodeData)

and to decode

local Data = datastore:GetAsync(key)
local decodeData = httpservice:JSONDecode(Data)
tix.Value = decodeData[1]
worlds.Value = decodeData[2]

Now, your entire script emplementing that:

local httpservice = game:GetService("HttpService")
local DataStoreService = game:GetService("DataStoreService")

local myDataStore = DataStoreService:GetDataStore("myDataStore")

local playersLeft = 0

game.Players.PlayerAdded:Connect(function(player)

    playersLeft = playersLeft + 1

    local leaderstats = Instance.new("Folder")
    leaderstats.Name = "leaderstats"
    leaderstats.Parent = player

    local tix = Instance.new("IntValue")
    tix.Name = "Tix"
    tix.Value = 0
    tix.Parent = leaderstats

    local worlds = Instance.new("IntValue")
    worlds.Name = "Worlds"
    worlds.Value = 0
    worlds.Parent = leaderstats

    local Data
    local success, errormessage = pcall (function()
        Data = myDataStore:GetAsync(player.UserId)
    end)

    if success then
       	local decode = httpservice:JSONDecode(Data)
	tix.Value = decode[1]
	worlds.Valie = decode[2]
        print("Player data loaded successfuly!")
    else
        print("There was an error whilst getting your data!")
        warn(errormessage)
    end
end)

local bindableEvent = Instance.new("BindableEvent")

game.Players.PlayerRemoving:Connect(function(player)
    local data = {player.leaderstats.Tix.Value,player.leaderstats.Worlds.Value}
    local success, errormessage = pcall(function()
        myDataStore:SetAsync(player.UserId,data)
    end)

    if success then
        print("Player data successfuly saved!")
        playersLeft = playersLeft - 1
        bindableEvent:Fire()
    else
        print("There was an error when saving data!")
        warn(errormessage)
    end

end)

game:BindToClose(function()
    while playersLeft > 0 do
        bindableEvent.Event:Wait()
    end
end)

Hope I explained it well!

2 comments

0

You can put data in a table, all of this is unnecessary.

0

@Dovydas1118 If you try to save a TABLE, it will give you an error, because it only saves int's, number's and strings, the jsonencode function encodes a table into a json string, so you can save because it is a string.

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