Loading a character save with a datastore?

Hello, i am trying to make an OC saving/loading system, so far im pretty okayish on the saving portion, but for the life of me i cannot figure out the loading part.
so im using a table to list out features that are important, the script im going to show is a test to just detect the character model type and then when you load it to morph you into the model you saved.

here is the saving portion of the script, this part is on a Server script, after firing the function from the client with a GUI button

save.OnServerEvent:Connect(function(player, Slot, model)
	
	
	local uniqueID = HttpService:GenerateGUID(false)
	
	playerSlots[uniqueID] = {
		Save = Slot, -- this is to check which slot they are saving in (I.E: save 1 or save 2 etc)
		Model = model,  -- this is to check the model they are, im using the numbers, 1 or 2, this is 1
		unique = uniqueID -- i dont think i need to save this but i just kept it here from previous attempts
	}
	
	print(playerSlots[uniqueID].Model, playerSlots[uniqueID].Save) -- to make sure it worked
	
	if	playerSlots[uniqueID].Save == 1 then -- Checking which save it is
		save1:SetAsync(playerSlots[uniqueID], player.UserId) -- saving the playerslots to the respective save datastore
		print("saved")
	end
	
end)

and then this is the loading portion, thats giving me the MOST trouble.

load.OnServerEvent:Connect(function(player, Slot)
	
	local save = Slot
	
	
	if	save == 1 then -- checking the save

		save1:GetAsync(player.UserId) -- grabbing the save
	
		print(playerSlots.Model) -- <- prints "nil"
		
		if playerSlots.Model == 1 then --- this is the morphing script VVV and it doesnt work
			print("model is a worker drone")
			local morph = game.ReplicatedStorage.Morphs.Live
			local char = morph:Clone()
			char.Name = player.Name

			player.Character = char
			char.Parent = workspace
		end
	end
end)

if i try to say

print(playerSlots[uniqueID].Model)

it says that it tried to index nil with model, i tried to save the uniqueID itself into the datastore but it says its “unable to cast array”.
there are a few other things i’ve tried that i dont remember but this whole thing has been pretty angering for me.

heres the full entire script if it helps, plus the client one.

--- server script
local save =  game.ReplicatedStorage.saving.Save
local load =  game.ReplicatedStorage.saving.Load
local players = game:GetService("Players")
local playerSlots = {}
local HttpService = game:GetService("HttpService")
local Data = game:GetService("DataStoreService")
local save1 = Data:GetDataStore("Save1")
local save2 = Data:GetDataStore("Save2")




save.OnServerEvent:Connect(function(player, Slot, model)
	
	
	local uniqueID = HttpService:GenerateGUID(false) -- creating a unique id
	
	playerSlots[uniqueID] = {
		Save = Slot, -- checking which slot they're saving
		Model = model,
		unique = uniqueID
	}
	
	print(playerSlots[uniqueID].Model, playerSlots[uniqueID].Save)
	
	if	playerSlots[uniqueID].Save == 1 then -- Checking which save it is
		save1:SetAsync(playerSlots[uniqueID], player.UserId) -- saving the playerslots to the respective save datastore
		print("saved")
	end
end)

---

load.OnServerEvent:Connect(function(player, Slot)
	
	local save = Slot
	
	
	if	save == 1 then
		save1:GetAsync(player.UserId)
	
		print(playerSlots.Model)
		
		if playerSlots.Model == 1 then
			print("model is a worker drone")
			local morph = game.ReplicatedStorage.Morphs.Live
			local char = morph:Clone()
			char.Name = player.Name

			player.Character = char
			char.Parent = workspace
		end
	end
end)
--- local script, attached to GUI buttons
local save = script.Parent.SAVE
local load = script.Parent.LOAD
local player = game.Players.LocalPlayer
local playerSlots = {}
local model

save.MouseButton1Click:Connect(function()
	local Slot = 1
	if player.Character.ID.Value == 1 then
	 model = 1
	else
	 model = 2
	end
	game.ReplicatedStorage.saving.Save:FireServer(Slot, model)
end)

load.MouseButton1Click:Connect(function()
	local Slot = 1
	game.ReplicatedStorage.saving.Load:FireServer(Slot)
end)

Note: im not sure how important it is, but i am not using a roblox model, im using a blender created model.

let me know if there’s anything i forgot to add or if you need to know

at the line
save1:GetAsync(player.UserId)
you aren’t actually holding onto the data your grabbing, you need to set something to it.
such as:

local slot = save1:GetAsync(player.UserId)
playerSlots[slot.unique] = slot -- updating your table
1 Like

So putting that in as it is and replacing what i had there, it just says that it attempted to index nil with unique

save1:SetAsync(playerSlots[uniqueID], player.UserId) -- saving the playerslots to the respective save datastore

so :SetAsync() works by Key, Value pairs. However your putting it in Value, Key.
Try swapping them around like so:

save1:SetAsync(player.UserId, playerSlots[uniqueID]) -- saving the playerslots to the respective save datastore

you’ll also need to add a if check to your loading function, as someone could be playing for the first time and not have any data yet.
like if not slot then return end for example.

Oh my goodness, this worked! thank you so much i cant believe it was that simple.

1 Like

I’ve seen you in a lot of datastore related posts recently and I think you should encourage them to add pcalls to their code. Some of em even save data in a weird way. I’m not saying it’s your job but they’ll be back here again if their problematic code stops working

1 Like

This is the first datastore post I’ve responded to in ages, It was more important to help them solve the fundamental issue they were having instead of confusing them with additional information right off the bat. As the purpose of this support post was the error they were encountering, which wouldn’t have been fixed by a pcall.

But yes, for the long term they should start wrapping their datastore methods with pcalls. (If you’re reading this livvy, a pcall() catches errors and lets you choose to end the code early without breaking other stuff. Here is a good post explaining them.)

2 Likes

I must’ve mistaken you for someone else mb

The other person also had a red profile like you… They ignored major issues that were easy to fix

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.