DataStore wont save multiple variables

I have 0 errors, here is the code;

local players = game:GetService('Players')
local Data = game:GetService("DataStoreService"):GetDataStore("CarSaveSystemDataSet")
players.PlayerAdded:Connect(function(player)
	local cars = {
		'Lykan HyperSport',
		'Mustang',
		'One:1',
	}
	local carFolder = Instance.new('Folder', player)
	carFolder.Name = 'Cars'
	for _,car in pairs(cars) do
		local val = Instance.new('StringValue', carFolder)
		val.Name = car
		val.Value = 'Unowned'
		end
	end)
game.Players.PlayerRemoving:Connect(function(plr)
    local p = {plr.Cars.Mustang.Value,plr.Cars['Lykan HyperSport'].Value,plr.Cars['One:1'].Value,}
	Data:SetAsync(plr.userId, p)
print(plr.Name.."'s Data was saved.")
end)

If you could please help, that would mean a lot. It just wont DataStore properly, and the values wont save.

The function connected to players.PlayerRemoving will not fire when the game is shutting down. This would be best fixed using game:BindToClose and binding a short wait(3) or so to let the save take place. That’s for a hacky solution though…

--after PlayerRemoving event
game:BindToClose(function()
    wait(3)
end)

There was another thread I helped with recently that was fleshing out the technicalities of this, I recommend you at least read the first paragraph and maybe even the solution:

This literally isn’t the issue… Because when I leave the server it prints that my data was saved, and then when I rejoin it just wont save.

You never call getAsync to load data. It saves, you never load it.

Try this:

players.PlayerAdded:Connect(function(player)
	local cars = {
		'Lykan HyperSport',
		'Mustang',
		'One:1',
	}
	local carFolder = Instance.new('Folder', player)
	carFolder.Name = 'Cars'
        local data = Data:GetAsync(plr.userId)
	for i, car in pairs(cars) do
		local val = Instance.new('StringValue', carFolder)
		val.Name = car
		val.Value = 'Unowned'
            if data[i]~='Unowned' then 
                 --Make owned
                 val.Value = 'Owned' --You might do it diferently, just a example.
                 end
		end
	end)
1 Like

I updated my reply above to get data. Note that I did not use pcalls for simplicity, but you should.

However you set values to be owned, do that where indicated.

1 Like

Sadly, this did not work. I will try and fix this.

Yeah, I made a typo (mobile is not my favorite). It should work now.

Full code:

local players = game:GetService('Players')
local Data = game:GetService("DataStoreService"):GetDataStore("CarSaveSystemDataSet")

players.PlayerAdded:Connect(function(player)
	local cars = {
		'Lykan HyperSport',
		'Mustang',
		'One:1',
	}
	local carFolder = Instance.new('Folder', player)
	carFolder.Name = 'Cars'
        local data = Data:GetAsync(plr.userId)
print(plr.Name.."'s Data is loading...")
	for i, car in pairs(cars) do
		local val = Instance.new('StringValue', carFolder)
		val.Name = car
		val.Value = 'Unowned'
            if data[i]~='Unowned' then 
                 --Make owned
                 val.Value = 'Owned' --You might do it diferently, just a example.
                 end
		end
	end)
game.Players.PlayerRemoving:Connect(function(plr)
    local p = {plr.Cars.Mustang.Value,plr.Cars['Lykan HyperSport'].Value,plr.Cars['One:1'].Value,}
	Data:SetAsync(plr.userId, p)
print(plr.Name.."'s Data was saved.")
end)

Worked, thanks bud. (30 character limit)

1 Like

No problem, glad I could help.

Sorry again for the typos, my auto correct does not like lua

That’s fine, much appreciated that you took time to help even while on mobile!

1 Like