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