Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How would I make this script delete the specific button I click on?

Asked by 5 years ago
Edited by SerpentineKing 5 years ago

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

2 answers

Log in to vote
0
Answered by
Ziffixture 6913 Moderation Voter Community Moderator
5 years ago
Edited 5 years ago

Reasoning


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.


Solution


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:)

0
https://pastebin.com/raw/nJZpr8VK is the new script i used. i the usernames still generate, but they're not clickable. I tried doin #Player.plr.PlayerGui.adminpanel.mainpanel.reportedplayers:GetChildren(), didn't work. currently the error (using the script linked in this comment), an error comes up in the console saying.. innocentbystander7 0 — 5y
0
"Players.innocentbystander7.PlayerGui.adminpanel.mainpanel.reportedplayers.LocalScript:76: attempt to index local 'Label' (a number value). Not sure what the issue is. innocentbystander7 0 — 5y
0
thank you again. :) innocentbystander7 0 — 5y
0
Give me a second while I look this over Ziffixture 6913 — 5y
View all comments (3 more)
0
Ha, didn't take long, I forgot that Label is represented as the current traverse Index, not the actual Object. It's just late for me, quite embarrassed here:) Try the updated Code. Ziffixture 6913 — 5y
0
new error says '1' is not a valid member of Model innocentbystander7 0 — 5y
0
Right, apologies again, the updated code should work Ziffixture 6913 — 5y
Ad
Log in to vote
0
Answered by
sheepposu 561 Moderation Voter
5 years ago
Edited 5 years ago

Try naming the button within the script and then deleting it. It will know which button to destroy.

Answer this question