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?

2 comments
You can put data in a table, all of this is unnecessary.
@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.