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

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

Asked by 7 years ago

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)
0
Do CharacterAdded instead so it fires everytime the character is added. FiredDusk 1466 — 7y
0
Why do you answer in comments ;-; Goulstem 8144 — 7y
0
Well, Im on my phone and I dont feeling like typing too much. I use the comments for more short answers too. FiredDusk 1466 — 7y

2 answers

Log in to vote
0
Answered by
Wiscript 622 Moderation Voter
7 years ago

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

Ad
Log in to vote
0
Answered by
Goulstem 8144 Badge of Merit Moderation Voter Administrator Community Moderator
7 years ago
Edited 7 years ago

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
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. jhunlzeus4 20 — 7y
0
It's fine, I don't necessarily need the rep :) Goulstem 8144 — 7y
0
I do cx Wiscript 622 — 7y

Answer this question