So basically I was working on a pet deletion system. When I was testing it out I realized that it had a problem. The problem was that if you would click a pet to delete, the pet would not be deleted unless it was the first pet that the Inventory displayed. Look at the video below to see what I mean.
You can see that I tried clicking on the 2nd pet, but I was not prompted with the text saying “Do You Want To Complete This Deletion” or something like that. When I clicked the First pet the Prompt popped up tho. I am pretty sure that the way I defined the template(which is the UI that displays the pet) is causing the problem.
I have 2 scripts that help the Pet Deletion System with its functionality.
script 1:Local Script
local delValue = del["off/on"]
local AutoDelText = game.Players.LocalPlayer.PlayerGui.Inventory.AutoDeleteText
local plr = game.Players.LocalPlayer
local Cancel = AutoDelText.Cancel
-- defining all the variables
del.MouseButton1Click:Connect(function()-- checking if you clicked the TrashCan/deletion tool
AutoDelText.Text = "Auto-Delete ON"-- displaying that you have deletion ON
delValue.Value = true
if delValue.Value == true then
AutoDelText.Visible = true
elseif delValue.Value == false then
AutoDelText.Visible = false
end
end)
Cancel.MouseButton1Click:Connect(function()-- block of code for turning off the deletion tool
delValue.Value = false
AutoDelText.Text = "Auto-Delete OFF"
AutoDelText.Visible = false
end)
script 2: Local Script
local Inv = game.Players.LocalPlayer.PlayerGui:WaitForChild("Inventory")
local template = Inv.LowerInvFrame.ScrollingFrame:WaitForChild("template")-- this is where I think the problem may be
local delValue = game.Players.LocalPlayer.PlayerGui:WaitForChild("Inventory").LowerInvFrame.TextButton.delete["off/on"]
local DeleteWarn = game.Players.LocalPlayer.PlayerGui.Inventory.DeleteWarn
-- defining all the variables
local MultiThread = coroutine.create(function()
-- making a courtine so the while while wait loop can run simultaneously with the MouseButton1Click Events
while wait(0.1) do
if delValue.Value == true then -- checking if deletion tool is turned
template.MouseButton1Click:Connect(function()
DeleteWarn.Visible = true -- if you click on the template the deletion message prompts you
end)
end
end
end)
local Yes = DeleteWarn.Yes
local No = DeleteWarn.No
-- defining variables of the DeletionMessage
Yes.MouseButton1Click:Connect(function()
print("Successful deletion")
template:Destroy()
wait(1)
DeleteWarn.Visible = false
end)
No.MouseButton1Click:Connect(function()
wait(0.1)
DeleteWarn.Visible = false
end)
coroutine.resume(MultiThread)
Tell me if you have any questions about anything. Your Time is Appreciated!
I think you’re right about that problem in script 2. I’m assuming the pet buttons are all named “template”. Whether they are or not, the template variable is only going to hold one button, so your prompt only works on that one button.
But there’s also a lot of other potential problems that might arise from your second script, so I had to kinda rewrite it. It’s not a perfect working script but it should provide you a rough idea of what to do:
local Inventory = Path_To_Wherever_Buttons_Are
local DeleteWarn = Path_To_Delete_Warning
local YesButton = Path_To_Yes_Button
local NoButton = Path_To_No_Button
local DeleteValue = Path_To_Delete_Value
local CurrentTemplateToDelete -- Value that will hold the button object to delete.
Yes.Activated:Connect(function()
if CurrentTemplateToDelete ~= nil then
CurrentTemplateToDelete:Destroy() -- Delete the selected button.
CurrentTemplateToDelete = nil -- Set variable to nothing so script won't get confused.
DeleteWarn.Visible = false -- Hide warning.
else
DeleteWarn.Visible = false -- If somehow the warning is shown with DeleteValue false, the warning will simply disappear and nothing will happen.
end
end)
No.Activated:Connect(function()
CurrentTemplateToDelete = nil
DeleteWarn.Visible = false
end)
-- Detect when new pets are added to the menu and detect button interaction.
Inventory.ChildAdded:Connect(function(obj)
if (obj:IsA("GuiButton")) and (obj.Name == "Template") then -- Check if object is a GUI button and if it's named "Template".
obj.Activated:Connect(function() -- Recommended to use .Activated over .MouseButton1Click for better compatibility with all devices.
if (DeleteValue.Value == true) then
CurrentTemplateToDelete = obj -- Let script know player may want to delete this button.
DeleteWarn.Visible = true -- Show deletion warning.
end
end)
end
end)
-- Only run this once, or you may end up creating multiple events for the same buttons!
for _, obj in pairs(Inventory:GetChildren()) do -- Get every object that is in the Inventory frame.
if (obj:IsA("GuiButton")) and (obj.Name == "Template") then
obj.Activated:Connect(function()
if (DeleteValue.Value == true) then
CurrentTemplateToDelete = obj
DeleteWarn.Visible = true
end
end)
end
end
Basically you’ll need to use a combination of for loops and events to properly make all your buttons work for deletion. I added comments to describe what everything does.
There are also some noticeable problems and recommendations with your original scripts:
It’s pointless to change your AutoDelText text if your text immediately gets hidden after. Just leave it at “Auto-Delete ON”.
You could simplify the visibility code of AutoDelText into one line by doing AutoDelText.Visible = delValue.Value.
As mentioned before, you’ll need to use for loops and/or events to detect the interaction of multiple buttons in your menu. Simply defining “template” to one object isn’t going to somehow make all the other buttons work.
You really, really should not use an infinite loop to create events for the buttons on your second script. The script is actually creating a new event for the button every 0.1 seconds while also maintaining the past created events. So pressing the button might end up running hundreds or more events which will create potential bugs and decreased performance in your game. Use the script I provided above to properly handle this.
The waits in your second script (except the while loop of course) are unnecessary.
This is somewhat a personal preference but I recommend combining your two local scripts into one and replacing your BoolValue object with a variable in the script.
Sorry if this is a bit lengthy, but there was a lot to explain. If you have any questions, let me know.