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

Somthing Wrong With This Inventory Script?

Hi I Have This Script That Is Supposed To Create A Custom Inventory That Can Hold 10 items. Obviously It Wont Work. Can Someone Help (Script Below)

local player = game.Players.LocalPlayer
repeat wait() until player.Character
local character = player.Character
local inventory = player.Backpack:GetChildren()
local amount = 0
	player.Backpack.ChildAdded:connect(function(child)
		if child:IsA("Tool") then
			amount = amount + 1
			if amount == 11 then
				wait(.1)
				child:Destroy()
			end
		end
	end)
	player.Backpack.ChildRemoved:connect(function(removed)
		if removed:IsA("Tool") and amount > 11 then
			amount = amount - 1
			print(amount)
		end
	end)

1 comment

0

Well, you haven't exactly told us what was wrong with it, but on line 16, flip that > and make it a <. Any other errors?

1 answer

Tip For The Future: You Need Not Capitalize Every Word Like This, Which Is In Fact Wrong To Do.

That aside, let's help you through the script, because you've clearly put effort to it before presenting it. Before you ask, that's a good thing.

Along with the character you will need to wait for the player too, unless you manually (via another script) give the script to the player.

To get the amount of items in a table, you can use # and cut yourself a variable (in this case, "amount"). For instance, #player.Backpack:GetChildren() will get the amount of items the player is carrying, supposing player is game.Players.LocalPlayer.

In cases where you are connecting to an event where a child/player is removed, it's ChildRemoving and PlayerRemoving accordingly. Instances don't exactly remove the instant they are called to, so the Roblox devs decided to name the event as such.

When you set a variable, the value doesn't update unless you set it again. So you'd have to set inventory every time it was required to get the accurate table of items.

I left the character variable in just in case you'll use it later.

repeat wait() until game.Players.LocalPlayer
local player = game.Players.LocalPlayer
repeat wait() until player.Character
local character = player.Character
--Inventory, whose items weren't used at all was removed.
--Amount is used for the number of items in the backpack in each event.
--Variables only update when you set them, with the exclusion of setting them
--to an existing object.
player.Backpack.ChildAdded:connect(function(child)
	local amount = #player.Backpack:GetChildren()
	if child:IsA("Tool") then
		if amount == 11 then
			wait(.1)
			child:Destroy()
		end
	end
end)
player.Backpack.ChildRemoving:connect(function(removed)
	local amount = #player.Backpack:GetChildren()
	if removed:IsA("Tool") and amount > 10 then -- Not sure what you want to do here
		print(amount)
	end
end)

1 comment

0

Thanks for the answer sadly it dosent work (And yes Doing This Is Just A Bad Habit) it says there is a problem with line 18 and i couldnt fifgure it out. did i miss somthing out?

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