Why this script don't save table?

Hi,

So i want to save a value of numbervalue that is inside every tool in my backpack, im using table for this, but for some reason this don’t save them. (I checked if table is saved using Datastore editor plugin)

game.Players.PlayerRemoving:connect(function(player)
	player.Character.Humamoid:UnequipTools()
	local valuesToSave = {}
	
	for _, v in pairs(player.Backpack:GetChildren()) do
		valuesToSave[v.Name] = v:WaitForChild("Amount").Value
	end
	
	MyDataStore:SetAsync(player.UserId, valuesToSave)
end)
1 Like

The connect is capital always if i am not wrong

:connect is deprecated but that doesnt mean it can’t be used, like Mouse for example so I dont think thats the problem

still don’t work

30 character needed…

Any errors in the output possibly?

1 Like

Another alternative method is:

game.Players.PlayerRemoving:connect(function(player)
	player.Character.Humamoid:UnequipTools()
	local valuesToSave = {}
	
	for _, v in pairs(player.Backpack:GetChildren()) do
		valuesToSave[v.Name] = v:WaitForChild("Amount").Value
             MyDataStore:SetAsync(player.UserId,v.name)
	end
end)

ist it will just save a name of last instance? as it will save not table but name and it will rewrite it for every tool so it will save only last tool name.

I am not very much sure if you can save tables as a whole with datastore and I haven’t tried it either

Oh ye! I was testing in actuall game so i couldn’t see errors after leaving but i tested in studio and it seems that i have error:
Screenshot_217

Is there any way i can save value of tool that equipped/inside character?

i will try add waitforchid

1 Like

I think Character might be removed before your script runs

Try to add waitforchild and see what happens then

Wait for child isn’t going to help it clearly says attempt to index nil which means Humanoid parent isn’t there

Yeah wait for the parent which is the character

local character = plr.Character or plr.CharacterAdded:Wait()

yeah… it didn’t gave any error but it don’t work

that’s not going to work either instead use

player.CharacterRemoving

i can use this function inside this one?

The reason why you are getting this error is because you spelt Humanoid wrong. Here is your code with the correct spelling:

game.Players.PlayerRemoving:Connect(function(player)
	player.Character.Humanoid:UnequipTools()
	local valuesToSave = {}
	
	for _, v in pairs(player.Backpack:GetChildren()) do
		valuesToSave[v.Name] = v:WaitForChild("Amount").Value
	end
	
	MyDataStore:SetAsync(player.UserId, valuesToSave)
end)

Instead of using SetAsync() you should look into using UpdateAsync() because

SetAsync() can be hazardous since it overwrites any value currently in the entry. If you’re updating an existing entry, UpdateAsync() is recommended because it considers the old value before making changes.” - Quoted from the developers hub

1 Like

There is 1 problem with this actually that It would fire everytime player respawn

He spelled humanoid wrong thats why it wasnt working.

lol didn’t noticed that. but it still don’t save