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.
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)
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)
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 .
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.
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.
The screenshot below shows the UI when the player goes to purchase the Item.
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!