Does this script work?

Hello!

I am working on a system where if the player owns an Item, the button to purchase that Item is no longer visible.

local button = script.Parent
local player = game.Players.LocalPlayer
local backpack = player.Backpack
local item = button.Parent.ItemName

local function check()
	if backpack:FindFirstChild(item.Value) == true then
		button.Visible = false
	else
		button.Visible = true
	end
end

check()

backpack.Changed:Connect(function()
	check()
end)

Please let me know if this will work, or if thereā€™s anything I need to change or adjust. By the way, this is in a local script inside the button.

you need to also check for the item inside the character i think

2 Likes

Here is an explanation

local button = script.Parent -- the button ofc
local player = game.Players.LocalPlayer --the player which the button is on the screen
local backpack = player.Backpack -- the backpack is where the player's tools are stored and can be equipped
local item = button.Parent.ItemName -- i believe this is the item that the player will be geven

local function check() -- this is a function that the script activates later
	if backpack:FindFirstChild(item.Value) == true then -- if the value for the item is true then it will do the follwing:
		button.Visible = false -- become invisible
	else
		button.Visible = true -- become visible
	end
end

check() -- function activates to see if the player own the item

backpack.Changed:Connect(function() -- when something enters or exits the players backpack, the function 'check()' will occur
	check()
end)
1 Like

This script should work but why canā€™t you just test it from Studio?

local button = script.Parent
local player = game.Players.LocalPlayer
local backpack = player.Backpack
local item = button.Parent.ItemName

local function check(prt: Instance)
if backpack:FindFirstChild(item.Value) ~= nil then
button.Visible = false
else
button.Visible = true
end
end

check()
backpack.ChildAdded:Connect(check)

1 Like

Oh yeah, Backpack.Changed would only check if a property of the Backpack is changed, not if a Child is added, this method will work.

1 Like

is ItemName a bool value? Because i am trying to replicate your thing but how will the ItemName be true or false?

1 Like

i have readjusted the script and tested it but i do not know what ItemName is. i assume it is a bool value but now can a bool value be in the player backpack and be in starter gui as indicated in this line:

local item = button.Parent.ItemName -- 'item' is in a screengui

if backpack:FindFirstChild(item.Value) == true then --'item' is in backpack

also i changed the last few line to this:

backpack.ChildAdded:Connect(function() -- checks if the tool is added into the backpack
	check()
	print('checked') -- for testing
end)
1 Like

The backpack:FindFirstChild(item.Value) == true part is unnecessary, as FindFirstChild will return either the instance if itā€™s found or nil if not. You can directly use the result in the if condition.

local button = script.Parent
local player = game.Players.LocalPlayer
local backpack = player.Backpack
local item = button.Parent.ItemName

local function check()
    if backpack:FindFirstChild(item.Value) then
        button.Visible = false
    else
        button.Visible = true
    end
end

check()

backpack.ChildAdded:Connect(function()
    check()
end)

backpack.ChildRemoved:Connect(function()
    check()
end)

Iā€™ve also changed backpack.Changed to backpack.ChildAdded and backpack.ChildRemoved .

1 Like

i think they should show us more about the system because i do not know what he is doing with backpack:FindFirstChild(item.Value) == true. It may be a value they are activating. I recommend using a bool value placed in starter player or starter character scripts as the value will be duplicated in the player or character models and can induvidually check if they are true or not.

1 Like

I can, Iā€™m not checking right now as Iā€™m still working on a few systems to ensure everything works. I wanted just to double check on the developer forum beforehand.

What does ā€œ(prt: Instance)ā€ mean? Iā€™ve added it to my script but it seems that the ā€œcheck()ā€ is being underlined with red.

ā€œItemNameā€ is a string value, the value of ā€œItemNameā€ is the Items name. E.g., for the Red Knife, the ā€œItemNameā€ value is ā€œRedKnifeā€.

Hereā€™s how the full system works:

  • The screenshot below shows the UI when the player goes to purchase the Item.
    image_2024-01-13_112058749

  • Inside the UI, there is a value called ā€œItemNameā€, which is a string value. The value of ā€œItemNameā€ is the name of the Item the player is buying. So in this case, itā€™s ā€œRedKnifeā€.

  • Thereā€™s a script inside the purchase button which allows the player to purchase the Item. This script fires a RemoteEvent, which adds the Item to their inventory.

  • The RemoteEvent grabs the ā€œItemNameā€ value, and finds the Item with the same name, again in this case, itā€™ll be the Red Knife, and adds it to their backpack.

  • The script below is what I made before any devforum input:

local button = script.Parent -- purchase button
local player = game.Players.LocalPlayer -- player (who's clicking the button)
local backpack = player.Backpack -- players backpack (where the Item is/isn't)
local item = button.Parent.ItemName -- string value 

local function check()
	if backpack:FindFirstChild(item.Value) == true then -- if the Item is in the backpack
		button.Visible = false -- hides the button so they can't purchase the Item again
	else
		button.Visible = true -- keeps the button visible so they can purchase the Item
	end
end

check()

backpack.Changed:Connect(function() 
	check()
end)
  • What this script is supposed to do is check if the player owns the Red Knife (if the Red Knife is in their backpack), and then makes the purchase button invisible (aka set visible to false), and if the Red Knife isnā€™t in their backpack, then the purchase button remains visible (aka visible is set to true, although it already is/should be visible if they donā€™t have the Item).

  • Note: I will change backpack.Changed to backpack.ChildAdded, and also add any other changes which have been suggested.

Hope this helps and feel free to ask any questions!