Rank Only Button

Hello. I’m making a change team GUI. And I have a couple of teams that are staff only. I would like to try and make it so that if you’re in a group or multiple of the extra group the text button would appear. If not it would destroy the button only not the whole GUI.

Here’s the script:
script.Parent.MouseButton1Click:Connect(function()

script.Parent.Parent.TestButton.Visible = not script.Parent.Parent.Frame.Visible

end)

if game.Players.LocalPlayer:GetRankInGroup(4403130) < 2 then

script.Parent.Parent:Destroy()

end

It always for some reason destroys the whole UI not just the text button. Could I get some help?

5 Likes
if game.Players.LocalPlayer:GetRankInGroup(4403130) < 2 then

script.Parent.Parent.TestButton:Destroy()

end

The issue is that you were calling :Destroy() on the UI, not the button.
Also, I’d recommend checking the group rank on the server too before letting them pick the team to prevent exploiters from being able to get the ranks without the role if you’re not already.

4 Likes

You made a simple error where you destroy the UI.

The second parent in this line is the UI. You must reference the TestButton in order to have the script destroy the Button.

script.Parent.Parent.TestButton:Destroy()

Maybe you could also put this with your line of code in order to prevent errors.

script.Parent.Parent:FindFirstChild(“TestButton”):Destroy()

Hope this helps!

1 Like

FindFirstChild should only be used if it’s not guaranteed the object you’re looking for exists.
If the object doesn’t exist, it just returns nil, and calling :Destroy() on nil will also just result in an error. All it’s doing is adding unneeded extra time to process the script.

edit 2/26/2021

1 Like

Sorry, I am new to scripting. Thanks for letting me know :slight_smile:

1 Like

I believe the issue is you did < instead of <= so Roblox just destroyed the gui.

script.Parent.MouseButton1Click:Connect(function()
script.Parent.Parent.TestButton.Visible = not script.Parent.Parent.Frame.Visible
end)
if game.Players.LocalPlayer:GetRankInGroup(4403130) < 2 then
script.Parent.Parent:Destroy()
end

That’s not the problem. That if statement is actually correct. What’s wrong is the line below it where they forgot to add .TestButton

How would the script look like after the fix?

Bit of an off-topic response, however I’d like to clarify that no, FindFirstChild can have a multitude of uses outside of being used only “if it’s not guaranteed the object you’re looking for exists”

Certainly one is for your own sanity, for example:

script.Parent:FindFirstChild("H",true)

Instead of:

script.Parent.A.B.C.D.E.F.G.H
1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.