Datasaveservice / datastore not working

Hello, I have tried to sort a data save out and I am unsure why this doesn’t work.

local DataStoreService = game:GetService("DataStoreService")

local Car_Save = DataStoreService:GetDataStore("Car_Save")


game.Players.PlayerAdded:Connect(function(player)
	local Cars = Instance.new("Folder", player)
	Cars.Name = "Cars_Info"

	wait(0.01)

	local Car_One = Instance.new("BoolValue", Cars)
	Car_One.Name = "CorsaVXR"
	Car_One.Value = Car_Save:GetAsync(player.UserId.."-CorsaVXR") or false
	
	local Car_Two = Instance.new("BoolValue", Cars)
	Car_Two.Name = "AudiA4Old"
	Car_Two.Value = Car_Save:GetAsync(player.UserId.."-AudiA4Old") or false
	
	local Car_Three = Instance.new("BoolValue", Cars)
	Car_Three.Name = "BMW3Series"
	Car_Three.Value = Car_Save:GetAsync(player.UserId.."-BMW3Series") or false
	
	local Car_Four = Instance.new("BoolValue", Cars)
	Car_Four.Name = "LandRover"
	Car_Four.Value = Car_Save:GetAsync(player.UserId.."-LandRover") or false
	
	local Car_Five = Instance.new("BoolValue", Cars)
	Car_Five.Name = "FordFocus"
	Car_Five.Value = Car_Save:GetAsync(player.UserId.."-FordFocus") or false
	
	local Car_Six = Instance.new("BoolValue", Cars)
	Car_Six.Name = "SubaruImpreza"
	Car_Six.Value = Car_Save:GetAsync(player.UserId.."-SubaruImpreza") or false



game.Players.PlayerRemoving:Connect(function(player)
	local success, errormessage = pcall (function()
			Car_Save:SetAsync(player.UserId.."-CorsaVXR",player.Cars_Info.CorsaVXR.Value)
			Car_Save:SetAsync(player.UserId.."-AudiA4Old",player.Cars_Info.AudiA4Old.Value)
			Car_Save:SetAsync(player.UserId.."-BMW3Series",player.Cars_Info.BMW3Series.Value)
			Car_Save:SetAsync(player.UserId.."-LandRover",player.Cars_Info.LandRover.Value)
			Car_Save:SetAsync(player.UserId.."-FordFocus",player.Cars_Info.FordFocus.Value)
			Car_Save:SetAsync(player.UserId.."-SubaruImpreza",player.Cars_Info.SubaruImpreza.Value)
	end)

	if success then
		print("Saved")
	else
		print("Failed")
			warn(errormessage)
		end
	end)
end)

I have used similar but I was hoping to try and get the values to save to one datastore.

Any Ideas?

Figured it out. You Can’t have multiple values to one datastore

I recommend using a table for this.

It looks like you have a syntax error in your code. You have an open parenthesis on the second line of your game.Players.PlayerAdded event, but it doesn’t have a corresponding close parenthesis. You also have an open parenthesis on the second line of the game.Players.PlayerRemoving event, but it doesn’t have a corresponding close parenthesis.

Here is the corrected version of your code:

game.Players.PlayerAdded:Connect(function(player)
	local Cars = Instance.new("Folder", player)
	Cars.Name = "Cars_Info"

	wait(0.01)

	local Car_One = Instance.new("BoolValue", Cars)
	Car_One.Name = "CorsaVXR"
	Car_One.Value = Car_Save:GetAsync(player.UserId.."-CorsaVXR") or false
	
	local Car_Two = Instance.new("BoolValue", Cars)
	Car_Two.Name = "AudiA4Old"
	Car_Two.Value = Car_Save:GetAsync(player.UserId.."-AudiA4Old") or false
	
	local Car_Three = Instance.new("BoolValue", Cars)
	Car_Three.Name = "BMW3Series"
	Car_Three.Value = Car_Save:GetAsync(player.UserId.."-BMW3Series") or false
	
	local Car_Four = Instance.new("BoolValue", Cars)
	Car_Four.Name = "LandRover"
	Car_Four.Value = Car_Save:GetAsync(player.UserId.."-LandRover") or false
	
	local Car_Five = Instance.new("BoolValue", Cars)
	Car_Five.Name = "FordFocus"
	Car_Five.Value = Car_Save:GetAsync(player.UserId.."-FordFocus") or false
	
	local Car_Six = Instance.new("BoolValue", Cars)
	Car_Six.Name = "SubaruImpreza"
	Car_Six.Value = Car_Save:GetAsync(player.UserId.."-SubaruImpreza") or false
end)

game.Players.PlayerRemoving:Connect(function(player)
	local success, errormessage = pcall(function()
		Car_Save:SetAsync(player.UserId.."-CorsaVXR",player.Cars_Info.CorsaVXR.Value)
		Car_Save:SetAsync(player.UserId.."-AudiA4Old",player.Cars_Info.AudiA4Old.Value)
		Car_Save:SetAsync(player.UserId.."-BMW3Series",player.Cars_Info.BMW3Series.Value)
		Car_Save:SetAsync(player.UserId.."-LandRover",player.Cars_Info.LandRover.Value)
		Car_Save:SetAsync(player.UserId.."-FordFocus",player.Cars_Info.FordFocus.Value)
		Car_Save:SetAsync(player.UserId.."-SubaruImpreza",player.Cars_Info.SubaruImpreza.Value)
	end)

	

In the code you provided, you have a number of events that are being connected to functions. The game.Players.PlayerAdded event is fired whenever a new player joins the game, and the function that is connected to this event is run with the new player as an argument. This function creates a folder called Cars_Info and a series of BoolValue objects inside of it, and sets the values of these BoolValue objects based on data stored in a datastore called “Car_Save”.

The game.Players.PlayerRemoving event is fired whenever a player leaves the game, and the function that is connected to this event is run with the player who is leaving as an argument. This function attempts to save the values of the BoolValue objects inside the player’s Cars_Info folder back to the “Car_Save” datastore.

The pcall function is used to wrap the code that is saving the values to the datastore in a “protected call”. This means that if any errors occur while the code inside the pcall function is running, the errors will be caught and the function will not crash. The success variable will be set to true if the code inside the pcall function ran without any errors, and false if there were any errors. The errormessage variable will contain the error message if there were any errors, or nil if there were no errors.

After the pcall function has run, a check is made to see if the success variable is true or false . If success is true , the message “Saved” is printed. If success is false , the message “Failed” is printed, and the error message is displayed using the warn function.

I hope this helps to clarify things! Let me know if you have any further questions.

That’s not the exact reason. You can save to multiple keys for one player, you just can’t do it all in a row because this causes you to exceed DataStore limitations (the script is probably screaming at you and telling you that you’re saving too much). As @DeEchteBelg suggested, you can put all the data inside of a table, save that table to one key and then load that table and the data inside of it.