Table Not Working!

Hey there!

I am having some trouble with tables

My issues: My table is not showing the items in it correctly. It doesn’t seem to save correctly as a result of that.

Ok, I am going to try to explain this really well so it is going to be a little easier for people to understand and respond.

Explanation of first issue: When I join the game, my table prints in the output from the server using this code:

print(_G.petUpgradeAmount[player.userId])

And it prints this:
Screenshot 2021-05-19 161930 unimportant, I am uncertain how “1” and “2” got there :laughing:
This is normal, and what I would expect it to print.

Every time a GUI needs that table information, in a local script of course, it needs to get it from the server.

It calls it through this line of code

local PetFunction = game.ReplicatedStorage:WaitForChild("PetUpgradeAmountRequest")
local Boost = PetFunction:InvokeServer()

The server then receives it and returns the table

local petUpgradeARequest = game.ReplicatedStorage:WaitForChild("PetUpgradeAmountRequest")

function onPetUpgradeARequest(player)
	while (_G.petUpgradeAmount[player.userId] == nil) do
		wait(0.1)
	end
	
	return _G.petUpgradeAmount[player.userId]
end

petUpgradeARequest.OnServerInvoke = onPetUpgradeARequest

That was all working grand and everything until a couple days ago, where it does seem to have enough information.

So I went to go print the table from the client to see what the problem was using some of the code up above:

local PetFunction = game.ReplicatedStorage:WaitForChild("PetUpgradeAmountRequest")

local Pet = script.Parent.Name -- the name of the gui this script is in is = one of the items in the table, for instance the "Default Pet"

local Boost = PetFunction:InvokeServer()

print(Boost[Pet], Boost)

I wanted to print the actual item with Boost[Pet] and the whole table with Boost, and I get this!
Screenshot 2021-05-19 163638
That is all there is!!! Like there is nothing else in the whole table!

I am certain that the information was not delete because at the same time, another server script that is connected to the actual pet and is calling the table as well via another method and it is printing the whole thing.

Conclusion: I know that was a lot, but I really am just stumped on why it is not working! I would really appreciate it if someone can help me find the solution. Thanks! If you need me to explain something more just ask!

Note: I am not going to try and explain the second issue, because I am guessing that will be solved when the first issue is fixed, but if it doesn’t then I will explain it.

According to this page…

Avoid passing a mixed table (some values indexed by number and others by key), as only the data indexed by number will be passed .

You need to find out what part of the code is inserting the zeroes and stop it. Alternatively you can just sanitize the table before sending it through the remote function to make sure it doesn’t contain any numerical indices:

function onPetUpgradeARequest(player)
	while (_G.petUpgradeAmount[player.userId] == nil) do
		wait(0.1)
	end
	local original = _G.petUpgradeAmount[player.userId]
	local copy = {}
	for key, value in pairs(original) do
		if (typeof(key) == "string") then
			copy[key] = value
		end
	end
	return copy
end
1 Like

Wow thanks I did not know that. Would it affect the way datastores save? Because it keeps resetting the table string items to 0.

I’m not quite sure about datastores, but I would do the same thing of making sure only string keys are used, just to be safe.

1 Like