Datastore not loading values.`

Have you tried doing this in a live game instead of in studio?

1 Like

You can replace this with:

for i, v in pairs(Table) do
    if table.find(inventory, i) then
        table.find(inventory, i).Value = v or 0
    end
end

And this:

With:

for _, child in ipairs(inventory) do
    if SaveDict[child.Name] then
        SaveDict[child.Name] = child.Value
    end
end --// make sure the value names are the same as the ones on the SaveDict.
2 Likes

Hey, testing this had no effect. I’m assuming the problem now lies with loading, but if it was then the values will be reset to 0, but in the DataStore Editor it says the correct values (somewhat). I am confused on why

This is what the plugin says

image

And this is what the output prints (after loading values)

image
(Where it says 0 is what the values are.)

Edit: Tried editing with the plugin and no effect

:frowning:

1 Like

Yep, problem with loading…

Ok let’s change some stuff yet again :sweat_smile: I forgot Instances can’t be gotten with their names but anyways.

for _, v in ipairs(inventory) do
    if Table[v.Name] then
        v.Value = Table[v.Name]
    end
end
1 Like

now this loads but it doesn’t save :frowning:

1 Like

Can you send me an updated script again with the new changes?

Here’s the saving

function module:SaveItems(player)
	local inventory = game.ServerStorage.PlayerValues[player.UserId].Inventory:GetChildren()
	local SaveDict = {
		["Teddy"] = 0,
		["Dragon"] = 0,
		["Panda"] = 0,
		["Bear"] = 0,
		["Deer"] = 0,
		["Pig"] = 0,
		["Monkey"] = 0,
		["Bunny"] = 0,
		["Dog"] = 0,
		["Cat"] = 0
	}


	for _, child in ipairs(inventory) do
		if SaveDict[child.Name] then
			print(child.Name)
			SaveDict[child.Name] = child.Value
		end
	end
	local success, errormsg = pcall(function()
		invData:SetAsync(player.UserId,SaveDict)
	end)

	if not success then
		warn(errormsg)
	else
		print("SAVED")
		
		for i,v in pairs(SaveDict) do
			print(SaveDict[i])
		end
	end
end

Just change this to:

for _, child in pairs(inventory) do
    SaveDict[child.Name] = child.Value
end

Also are you sure everything has the same name and everything?

Also, anything about the script you’re calling these functions from?

1 Like

Tried this same issue, won’t save, but it loads.

Yes they are the exact names

Its just a playeradded event to load, and a playerremoving to save.

Anyhow, can I see it?

Idk then.

game.Players.PlayerAdded:Connect(function(player)
	local playerFolder = script.Folder:Clone()
	playerFolder.Name = player.UserId
	playerFolder.Parent = game.ServerStorage.PlayerValues

	--GetHatchedData(player)

	Inventory:LoadItems(player)
end)

game.Players.PlayerRemoving:Connect(function(player)
	local playerFolder = game.ServerStorage.PlayerValues[player.UserId]
	
	--[[
	for i,v in ipairs(playerFolder.HatchedData:GetDescendants()) do 
		if v:IsA("IntValue") then
			local name = v.Name
			local user = player.UserId

			if v.Value ~= 0 and v.Value ~= hatchedData:GetAsync(user.. name) then
				local s, e = pcall(function()
					hatchedData:SetAsync(user.. name, v.Value)
				end)

				if not s then warn(e) end

				Inventory:SaveItems(player)
			end
		end
	end
	--]]
	
	Inventory:SaveItems(player)
	wait(1)
	playerFolder:Destroy()
end)

ignore the commented out code thats for saving something different, the inventory is handled with a module script/

I have an idea as to why this doesn’t work. I think if you are in Roblox Studio, and you press Stop, the player removing event seems to have some problems. Either test this on the Roblox Player, or add an autosave that saves in an interval (60 seconds recommended). Hope this helps!

1 Like

I think it’s more of a problem of him not having a BindToClose() or something.

1 Like

The server shutdowns whenever you leave the game in Studio, which is exactly the reason you need to save the data on DataModel | Documentation - Roblox Creator Hub.

game:BindToClose(function()
    for _, player in ipairs(game.Players:GetPlayers()) do
        -- save data here in a new thread
    end
end)  
1 Like

I feel like an idiot. I didn’t think that a bind to close would mess with this, but ig it did. Thanks for this, and thanks to @IEnforce_Lawz, @LucasTutoriaisSaimo, and whoever else helped me! TYSM :slight_smile:

1 Like

I would recommend only doing a wait() when on Studio, and then if it’s not studio, loop through every player and save their data. On Studio, the problem is mostly the server closing before the player removing event happens.

1 Like

hahhahhhahahahahaha funny moment the datastore is literally duplicating the values idk why

1 Like

Probably a issue on another script which deals giving a player pets and such. I would create another topic.

Most likely isn’t, the same script has a function that gives the player the amount of pets based on the datastore, it’s something like

for i = 1, dataValue, 1 do

loop. I can double check but I dont know why itll be doing this

1 Like

Does the :AddItem() function add a value into the player’s inventory? If yes, then the loop to load the data would use that function instead.