Variable assigning another even if they have no correlation to eachother?

I’ve encountered quite possibly the most confusing bug today and I still haven’t managed to fix it. For some odd reason, selectingItem is being overwritten by another variable, despite not even being called in the getChestInfo function??

Here, take a look.

function getchestinfo(slot, info)
	if chestInspecting and UIup == true and gui.chest.Visible == true then
		local folder = chestInspecting.chest.StoredItems
		if folder:FindFirstChild("slot"..slot) then
			local slotFound = folder:FindFirstChild("slot"..slot)
			local returningData = slotFound
			if info == true and slotFound.ItemName then
				local SaveThisData = {}
				SaveThisData = itemmodule.ReturnInfo(slotFound.ItemName.Value)
				SaveThisData.slot = slot
				if selectingItem then
					--prints the selectingItem health (currently unmodified)
					print(selectingItem.health)
				end
				if slotFound:FindFirstChild("Health") then
					SaveThisData.health = slotFound.Health.Value
				end
				if selectingItem then
					--prints the selectingItem health, but it gets modified despite it only changing the SaveThisData variable???
					print(selectingItem.health)
				end
				if slotFound:FindFirstChild("StackNumber") then
					SaveThisData.amount = slotFound.StackNumber.Value
				end
				returningData = SaveThisData
			end
			return returningData
		end
	end
end

Noticed a issue? Probaly not. Nothing else in the script is modifying the selectingItem variable, but it still does it anyway.

What data is selectingItem assigned before this function? If it’s not being modified anywhere else, it seems it must be connected to SaveThisData.health in some way.

It may also help if you show the output and add a print for the output of SaveThisData.health as well.

selectingItem is a variable that shows which item the player is holding while looking in a chest. It’s modified when the player drops the item or puts in a slot of the Chest. In this case, this bug happens when the player has a item selected, and clicks on another item (which will swap them), but if both items are the same, and one of them has lower health, it will just end up setting its health to the one that just swapped, causing this bug.

It seems to modify the health value specifically when the getChestInfo function is called. Despite it only being a function that returns the info of the item in the Chest.

image

The module called has this function:

function module.ReturnInfo(name)
	if name:match(" BreakableBlock") then
		name = string.gsub(name, " BreakableBlock", "")
	end
	local found = nil
	for i,v in pairs(blocks) do
		if v.name == name then
			found = v
		end
	end
	for i,v in pairs(melee) do
		if v.name == name then
			found = v
		end
	end
	for i,v in pairs(food) do
		if v.name == name then
			found = v
		end
	end
	if found then
		return found
	end
end

I may be wrong but since slotFound is a folder and returningData is suppose to be dictionary, but what your sending if this stuff is false

you would be just sending the folder instead of the contents of the folder

The function is sent with true as the second parameter (aka info).

just a quick test what does

this print out before doing this

It prints out that screenshot of the output above that I send to the other guy.

Well I have no clue on what its going on but maybe somewhere else in your code it might’ve changed the chest storage value gl though.

1 Like

Turns out that just setting a variable to something from another table causes bugs, so i had to copy all the properties to a new table. And that fixed it, good golly. That took HOURS.

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