Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Making A Inventory Gui And When You Get A New Item It Makes Two Of The Frames?

It Makes Two Frames Showing The Same Item When I Insert A Item Into PlayersItems Folder.

Script:

local Player = game.Players.LocalPlayer
local Inventory = script.Parent.Shadow.Inventory
local Item = script.Parent.Shadow.Item
local PlayerItems = script.Parent.PlayersItems
local GameItems = script.Parent.GameItems
local TotalItems = {}
local HasAItem = false

PlayerItems.ChildAdded:Connect(function(child)
	if #TotalItems == 0 and not HasAItem then
		HasAItem = true
		local ItemF = Item:Clone()
		ItemF.ItemImage.Image = "rbxassetid://"..child.Icon.Value
		ItemF.Parent = Inventory
		ItemF.Visible = true
		table.insert(TotalItems,child.Name)
	elseif #TotalItems < 7 then
		local ItemF = Item:Clone()
		ItemF.ItemImage.Image = "rbxassetid://"..child.Icon.Value
		ItemF.Parent = Inventory
		ItemF.Visible = true
		table.insert(TotalItems,child.Name)
		if #TotalItems >= 7 then
			ItemF:Destroy()
			child:Destroy()
		end
	else
		local ItemF = Inventory[child.Name]
		if ItemF then ItemF:Destroy() end
		child:Destroy()
		print("Max Inventory Space!")
	end
end)

Doesn't Print A Error.

2 answers

✓ Accepted answer

At the end of each if do a return or break so that it doesnt reach the other if and clone it again

if #TotalItems == 0 and not HasAItem then

elseif #TotalItems < 7 then

both of these can be true at once, so you have to break/return the function so it doesnt repeat itself :)

Put your debounce in an if statement outside of the rest of the code. Debounce should work reguardless of #TotalItems. Have your if statement check if #TotalItems is less than 7 but more or equal to 0 to create, and use the else statement to destroy.

######note: I think you should look into UIGridStyleLayouts

local Player = game.Players.LocalPlayer
local Inventory = script.Parent.Shadow.Inventory
local Item = script.Parent.Shadow.Item
local PlayerItems = script.Parent.PlayersItems
local GameItems = script.Parent.GameItems
local TotalItems = {}
local HasAItem = false

PlayerItems.ChildAdded:Connect(function(child)
	if not HasAItem then
		HasAItem = true;
		if #TotalItems >= 0 and #TotalItems < 7 then
			local ItemF = Item:Clone()
			ItemF.ItemImage.Image = "rbxassetid://"..child.Icon.Value
			ItemF.Visible = true
			ItemF.Parent = Inventory
			table.insert(TotalItems,child.Name)
		else
			local ItemF = Inventory[child.Name]
			if ItemF then ItemF:Destroy() end
			child:Destroy()
			print("Max Inventory Space!")
		end
		HasAItem = false;
	end
end)

This discussion is preserved as it was. New answers, comments, and votes are closed.