I'm trying to make a datastore for a game but it's giving me some trouble. I'm pretty new to datastore, I made this script and have been working on it for a while but I still can't seem to fix it. Any Idea's as to why is not working?

Server Script in ServerScriptService:

local DSS = game:GetService("DataStoreService")
local RepStorage = game:GetService("ReplicatedStorage")
local saveData = RepStorage.saveData
local RedlinePlayerDS = DSS:GetDataStore("RedLineRacing[Datastore]")
local Tutorial = RepStorage.TutorialEvent

SessionData = {}

local PlayerDataTemplate = {
	--["Name"] = {"ValueType"},
	["Rowg Coins"] = {"NumberValue", 15000},
	["Miles Driven"] = {"NumberValue", 0},
	["Rank"] = {"StringValue", "Permit"},
	["Ae86"] = {"BoolValue", false},
	["180SX"] = {"BoolValue", false},
	["1968 Challenger"] = {"BoolValue", false},
	["911"] = {"BoolValue", false},
	["Accord"] = {"BoolValue", false},
	["Camaro SS"] = {"BoolValue", false},
	["Corvette Z06"] = {"BoolValue", false},
	["Huracan Performante"] = {"BoolValue", false},
	["LFA"] = {"BoolValue", false},
	["Lancer Evolution"] = {"BoolValue", false},
	["Miata"] = {"BoolValue", false},
	["ModelS"] = {"BoolValue", false},
	["Senna"] = {"BoolValue", false},
	["Supra"] = {"BoolValue", false}
}

game.Players.PlayerAdded:Connect(function(player)
	--Variables
	local playerKey = "player_"..player.UserId
	local CarStorage = Instance.new("Folder", player)
	CarStorage.Name = "OwnedCars"
	local leaderstats = Instance.new("Folder",player)
	leaderstats.Name = "leaderstats"
	
	--simple generic-for loop to make new value instances:
	for name, object in pairs(PlayerDataTemplate) do
		local newValue = Instance.new(object[1])
		newValue.Name = name
		newValue.Value = object[2]
		
		if object[1] == "NumberValue" or object[1] == "StringValue" then
			newValue.Parent = leaderstats
		else
			newValue.Parent = CarStorage
		end
	end
	
	local GetSave;
	local success, message = pcall(function()
		GetSave = RedlinePlayerDS:GetAsync(playerKey)
	end)
	
	if success then
		--DataStore worked and retrieved something. We continue.
		if GetSave then
			SessionData[playerKey] = GetSave
			for _, object in ipairs(CarStorage:GetChildren()) do
				if object:IsA("BoolValue") then
					print(object.Name, GetSave[object.Name])
					object.Value = GetSave[object.Name]
				end
			end
			for _, object in ipairs(leaderstats:GetChildren()) do
				if object:IsA("StringValue") or object:IsA("NumberValue") then
					print(object.Name, GetSave[object.Name])
					object.Value = GetSave[object.Name]
				end
			end
			print("Data loaded for "..player.Name)
		else
			print("Welcome "..player.Name.." To RedLine Racing!")
			SessionData[playerKey] = {
				["Rowg Coins"] = 15000;
				 ["Miles Driven"] = 0;
				 Rank = "Permit";
				Cars = {
					["Ae86"] = false;
					["180SX"] = false;
					["1968 Challenger"] = false;
					["911"] = false;
					["Accord"] = false;
					["Camaro SS"] = false;
					["Corvette Z06"] = false;
					["Huracan Performante"] = false;
					["LFA"] = false;
					["Lancer Evolution"] = false;
					["Miata"] = false;
					["ModelS"] = false;
					["Senna"] = false;
					["Supra"] = false;
					}
			}
		end
	else
		warn("Can't Load Player Data! Reload Server.")
	end
end)

game.Players.PlayerRemoving:Connect(function(player)
	print(player.Name.." Has Left!")
	local playerKey = "player_"..player.UserId
	local CarStorage = player.OwnedCars
	local leaderstats = player.leaderstats
	local GetSave = RedlinePlayerDS:GetAsync(playerKey)
	
	for _, object in ipairs(CarStorage:GetChildren()) do
				if object:IsA("BoolValue") then
					SessionData[playerKey].Cars.object.Name = object.Value
				end
			end
			for _, object in ipairs(leaderstats:GetChildren()) do
				if object:IsA("StringValue") or object:IsA("NumberValue") then
					SessionData[playerKey].object.Name = object.Value
				end
			end

	local success, message = pcall(function()
		RedlinePlayerDS:SetAsync(playerKey, SessionData[playerKey])
	end)
	
	if success then
		print("saved success for "..player.Name)
	else
		warn("Did not save because: "..message)
	end
end)

4 comments

0

What do you mean by "it's not working"? Is the script printing an error, is the leaderstats not being created, or is it not saving?

0

It's not saving, and it prints: bad argument to line 71, string expected got nil.

0

In the script you posted, line 71 just has an "end" on it. What line does the error occur on in the script you posted? If it's line 63 or 69 then the save might be missing a value you expect to be there (consider providing a default - a useful practice for future versions of your script, where you may save more stuff))

0

So I removed some empty spaces, the line is newValue.Value = object[2], I thought I did provide some default values in player data template. If not how do I do so?