here's the full script for anyone who needs it: https://pastebin.com/raw/JwGVJnu8
okay so as you can see in the pastebin above, it generates textbuttons every second and names the generated buttons one of the names pulled out of the table, and when you click on one of the buttons, that specific button you click is supposed to delete itself, but instead, it deletes a random button that was generated. I've tried everything that I know of, including:
plr.PlayerGui.adminpanel.mainpanel.reportedplayers[label.Text].MouseButton1Click:Connect(function() plr.PlayerGui.adminpanel.mainpanel.reportedplayers[label.Text]:Destroy() end)
and:
label.MouseButton1Click:Connect(function() label:Destroy() end
and more that I can't remember of right now. If you could show me how I could go about fixing this, that'd be great. And yes I definitely tried making this script work, I've spent about 2 hours just trying to make this script work, to no avail obviously.
Thank you!
[SerpentineKing]: Edited Code Structure
Classic logical issue. The cause of this claimed "random" deletion isn't random. You see, the while loop
has defined Label
as the reference to the TextButton
it created. Therefore when using Label.Text
, which should preferably be Label.Name
, it isn't saying "Destroy the clicked Button", it actually is saying, "Destroy the clicked button I just made".
Another potential issue is with matched Names. The Listener is asking to find the Label
of it's Name, which there likely would be copies. It would end up Indexing the very last one of it's Name, instead of the copy clicked.
Instead of using a Listener inside of your loop, create another loop traversing through the existing labels. This will allows you to check over every Label
for a Click Signal.
Though this wouldn't work as the while loop
would be running, yes? Well, we can wrap the loop in a spawn
as you had done inside, though it was incorrectly used, so please remove it, and it's nested Code.
local ReportedPlayers = Player. -- Pathway to ReportedPlayers | Use as shortcut. spawn(function() while true do wait(1) --// Better do do it like so. --// Label creation end end) for Label = 1,#ReportedPlayers:GetChildren() do ReportedPlayers:GetChildren()[Label].MouseButton1Click:Connect(function() Label:Destroy() end) end
Hope this helps! If so, don't forget to accept this answer!, and optionally upvote:)
Try naming the button within the script and then deleting it. It will know which button to destroy.