My code has dementia and I'm fed up with it

Hello. I am working on an inventory system for a game prototype and my code is doing weird things. (Said every programmer ever)

So for whatever reason I’m getting an “attempt to index nil” error for some data for the slots that is being sent to the client from the server but when I print said data on the line above it, it prints just fine.

AddItemRemote.OnClientEvent:Connect(function(SlotIndex, SlotData, SlotLocation, AddMode)
	
	local Location : Frame = UI:FindFirstChild(SlotLocation)
	local Slot = Location:FindFirstChild(tostring(SlotIndex))
	
	print(SlotData["StackCount"]) -- Prints data perfectly fine
	Slot.StackCount.Text = tostring(SlotData["StackCount"]) -- throws error: "attempt to index nil with 'StackCount'"
	
	Slot.Image = ImageIDs[SlotData["Item"]["ID"]]
	
end)

Probably should note that this code fires when an item is attempted to be picked up. It also adds the item to the inventory’s backend on the server just fine but when trying to display it in the UI it has this issue.

I have been staring at the code for like half an hour and cannot figure out what the issue is.
If anyone knows what the issue is or has any ideas I would very much appreciate it.

Respectfully in tears,
– Bitterman1021

6 Likes

Are you sure Slot is not nil? How about Slot.StackCount?

5 Likes

Same error. Also prints the slot just fine if I do:

print(SlotData)

(My god I’m too tired to edit my comment’s properly…)

3 Likes

Thanks for the help. I genuinely have never encountered anything like this before and I am out of ideas.

3 Likes

I know it was already mentioned, but it has to be that Slot is nil, so Slot.StackCount is failing. (or im reading it wrong)
Please print Slot and double-check it’s not that

4 Likes

I see what you mean. That’s what the error means. But it just doesn’t make sense that if it thinks Slot is nil that it would print it perfectly fine

Did this and Slot does indeed exist

Thank you for the help

EDIT: Misread a bit. Lemme test that mb. I’m too tired for this

2 Likes

Try running the tostring() in the print function too. If it throws an error, it could be something to do with converting it to a string.

Another question, what type of value are you getting? A string or a num? I’m assuming a num.

I’m not entirely sure on how this is happening either, your code logic seems textbook to me.

3 Likes

I AM A MORON
You were right. I was under the assumption that the “StackCount” it was referring to was the one in the

SlotData["StackCount"]

But it was actually the Slot UI item

However I still don’t understand why it thinks the Slot UI Item doesn’t exist but I am looking into it. Thank you very much

EDIT: Figured it out and it is working now. Thank you very much.

4 Likes

Oh that is quite the clever and deeper way to look at this. Doesn’t help that it’s named the same way, lol.

5 Likes

Yeah that was probably unwise on my part tbh lol

3 Likes

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