Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How do I make a script that removes a ScreenGUI if not a certain rank?

I am trying to make a GUI that only certain members of a group can access. I have been testing my script and when I check the output I get no errors. I put the code in a Localscript so I do not know why it is not working. Any help would be appreciated, the script is below.

local Person = game.Players.LocalPlayer
local Rank = Person:GetRankInGroup(3350111)

function onPlayerRespawned(newPerson)
	if Person:IsInGroup(3350111) then
		if Rank < 10 then
			Person.PlayerGui.StatusControl:Destroy()
		end
	else
		Person.PlayerGui.StatusControl:Destroy()
	end
end

game.Players.PlayerAdded:connect(onPlayerRespawned)

3 comments

0

Do CharacterAdded instead so it fires everytime the character is added.

0

Why do you answer in comments ;-;

0

Well, Im on my phone and I dont feeling like typing too much. I use the comments for more short answers too.

2 answers

✓ Accepted answer

I've changed your code a bit. It's quite simple now. The comments explain what's happening.

We use the event CharacterAdded because of the player respawning. If they respawn, we still don't want the gui in it.

THE SCRIPT BELOW IS A SERVER SCRIPT AND NOT LOCAL PLACE IT IN SERVERSCRIPTSERVICE!

local groupId = 3350111 -- Get the group

function destroyGui(gui) -- Variable GUI
	gui:Destroy() -- Destroy it
end

game.Players.PlayerAdded:connect(function(p) -- When a new player is added to the game
	local rank = p:GetRankInGroup(groupId) -- Gets rank
	p.CharacterAdded:connect(function(char) -- When the player respawns
		repeat wait() until char -- repeat trying to do the process until the character is spawned
		if p:IsInGroup(groupId) then -- If the player is in the group
			if rank < 10 then -- if the rank on that group is lower than 10 then
				destroyGui(p:WaitForChild("PlayerGui"):FindFirstChild("StatusControl")) -- Destroy the gui
			end
		else -- If player isn't in group then 
			destroyGui(p:WaitForChild("PlayerGui"):FindFirstChild("StatusControl")) -- Destroy it anyways
		end
	end)
end)

If I helped, accept my answer :D

When they player's character spawns, everything from StarterGui is cloned into the player's PlayerGui. Attempting to check and remove upon joining isn't going to do anything because nothing is there yet :)

You don't even need a PlayerAdded event. If this is a localscript as a descendant of the ScreenGui you wish to destroy, the script is going to load everytime the character does.

You can simply do this:

local Person = game.Players.LocalPlayer
local Rank = Person:GetRankInGroup(3350111)

if Rank < 10 then
	script.Parent:Destroy() --Make sure path is correct
end

3 comments

0

This answer worked as well, but unfortunately it would not let me accept it as well, sorry. Thanks anyways though, I really appreciate it.

0

It's fine, I don't necessarily need the rep :)

0

I do cx

This discussion is preserved as it was. New answers, comments, and votes are closed.