Creating Inventory GUI via Script

Hey Devs,

I am attempting the create an Inventory system that updates the Inventory GUI as the player purchases something.

Currently I create a folder that saves/loads when the player joins and leaves, this how is how I am saving their purchases.

Whenever the player purchases something, a BoolValue is added to the folder and set to true.

I have been trying to figure out a way to monitor for when this Value is added to the folder, so that I can then change the player’s Inventory GUI, and add the Item into it.

This is the script that I am currently using to check for the change:

Summary
local player = game.Players.LocalPlayer
local playergui = player.PlayerGui
local leaderstats = player.leaderstats

local shopvalues = player.ShopValues
local inventoryvalues = player.InventoryValues

local animations = inventoryvalues.Animations
local effects = inventoryvalues.Effects
local phones = inventoryvalues.Phones
local robux = inventoryvalues.Robux


-- Creates tables of all Player Items
local animitems = {}
local effectitems = {}
local phoneitems = {}
local robuxitems = {}


-- These add the Player's Inventory Items to tables; will create the Inventory GUI later using the tables
for i, v in pairs(animations:GetChildren()) do
	table.insert(animitems, i, v)
end

for i, v in pairs(effects:GetChildren()) do
	table.insert(effectitems, i, v)
end

for i, v in pairs(phones:GetChildren()) do
	table.insert(phoneitems, i, v)
end

for i, v in pairs(robux:GetChildren()) do
	table.insert(robuxitems, i, v)
end


-- If values change, then add new ones the tables
animations.Changed:Connect(function()
	for i, v in pairs(animations:GetChildren()) do
		table.insert(animitems, i, v)
	end
	
	game.ReplicatedStorage.RemoteEvents.NewInventory:FireServer()
end)

effects.Changed:Connect(function()
	for i, v in pairs(effects:GetChildren()) do
		table.insert(effectitems, i, v)
	end
	
	game.ReplicatedStorage.RemoteEvents.NewInventory:FireServer()
end)

phones.Changed:Connect(function()
	for i, v in pairs(phones:GetChildren()) do
		table.insert(phoneitems, i, v)
	end
	
	game.ReplicatedStorage.RemoteEvents.NewInventory:FireServer()
	
end)

robux.Changed:Connect(function()
	for i, v in pairs(robux:GetChildren()) do
		table.insert(robuxitems, i, v)
	end
	
	game.ReplicatedStorage.RemoteEvents.NewInventory:FireServer()
end)

However, Changed does not seem to be working correctly with what I want to happen.

I previously used Prints check if this code was working, and they were inside of the Changed functions. I checked the Explorer to make the sure the Values were being added to the correct folder after the player purchased an item, and they were; but the print functions did not work –

UNTIL I Stopped the Test, then the Prints were inside of the output. I have never really had an issue like this before, so I am not really sure what I should do to get around it.

I am assuming that Changed will just not work with what I am trying to do, but I have never had an issue with them doing this before, and it is really weird to me that they are only firing after I stop the test.

If anyone can give me some insight on a better to check for a change within the folder, or help me figure out why I am having this issue; and overall get me moving in the right direction again, please feel free to help in anyway! Thanks!

Are you trying to make it fire when the BoolValue is parented to the folder? If so, try ChildAdded instead. If you’re trying to make it fire when the value changes in the BoolValue, try GetPropertyChangedSignal.

2 Likes