Making a GUI visible for a group only


#1

So what I want to do is basically to make a textbutton visible only for somoene inside a specific group with a specific rank(above).

here is what I’ve managed to write:

game.Players.PlayerAdded:connect(function(Player)
    if Player:GetRankInGroup(2831912) == 255 then script.Parent.Visible = true

    end
end)

I’ve also tried

game.Players.PlayerAdded:connect(function(player)
    local groupid = 2831912

    if player:IsInGroup(2831912) and player:GetRoleInGroup(2831912) >= 255 then
        script.Parent.Visible = true	
    end
end)

nothing has worked, Can someone point out the issue please? also please consider that I’m very new into scripting (2 weeks) so please don’t explain things too professionally for me there is a lot of things I don’t know and haven’t tried yet.

Thanks!


#2

First off, you don’t want to have this connected to the PlayerAdded event. The way you have it set up, if any person in your group joins the game, Everyone will be able to see that gui.

Second, you don’t need it connected to any event or function, you can just write it like so:

local player = game.Players.LocalPlayer
local groupId = 2831912

if player:IsInGroup(groupId) then
   script.Parent.Visible = true
end

If this code is run in a local script then it will work as long as the group id is correct. You also need to make sure the local script is a child of the gui, but I think you already have that based on your previous code.


#3

Here is another method to do it which is very slightly more secure since it destroys the GUI (assumed the parent), which you may find useful unless the GUI has other elements for the public. If that’s the case then you can just destroy the TextButton instead.

local plr = game.Players.LocalPlayer
local group, rank = 2831912, 255

if plr:GetRankInGroup(group) == rank then
    script.Parent.Visible = true
else
    script.Parent.Parent:Destroy()
end

#4

This introduces issues, such as players who aren’t in the group being able to see the gui through other methods.


Instead, do hook it up to PlayerAdded. However, when you check the player’s rank, you want to use GroupService to do so. GroupService always returns up-to-date data on a player’s groups. I don’t know the name of this function and would need to be on my computer (which I’m not right now) to give a proper explanation.

The steps are:

Connect PlayerAdded event
When the player joins the game, check their rank in the group using a certain GroupService method
If they are in the group and are a sufficient rank to see the gui, clone a copy of the gui into the player’s PlayerGui

Put the server script in ServerScriptService. Put the gui in the script. If the problem isn’t solved later when I’m on my computer I will give a more thorough explanation.


#5

I also suggest you add even more protection to your game by adding this small amount of code into a LocalScript and then place it in StarterPlayerScripts. What it does is it basically prevents other users from changing their ID and getting access to any other stuff you have limited to certain users.

local plr = game.Players.LocalPlayer

plr.Changed:Connect(val)
    if val == "UserId" then
       --You can fire a RemoteEvent here that sends a report to you via Trello or Discord
        plr:Kick()
    end
end)

#6

If they change it, it would be on the client side. If you’re checking on the client, they can just remove the code doing so, so this isn’t really protection.

Its better to just make all the checks from the server and not try to check from client with some client sided checks. For example, in this situation:

  • Connect PlayerAdded to function
  • Connect CharacterAdded to function
  • Replicate the ScreenGui to their PlayerGui (This way it is seen on the server)
  • If they are in group, replicate the button only visible to group into the correct frame.

Of course, these Guis would be stored on the server and not seen by the clients. This way, it wouldn’t matter if they changed themselves on the client end, they’ll never actually see the ui they are not meant to see because its not replicated on the client.


#7

To make the GUI visible for only group members, make a script in ServerScriptService. Then, hook up a PlayerAdded event. Depending on whether or not ResetOnSpawn of the GUI is true or not, you may need to add a CharacterAdded event. Though for my explanation, I’ll assume ResetOnSpawn is false, as it should be in most cases (from my experience).

Whenever the function runs (player enters), you want to check if the player is in the group. To do this, use the GetGroupsAsync function of GroupService to get an array of the player’s groups. Use a for loop to look through each of the groups, and compare the ID of each of them to your group ID. If the group ID matches, check if the rank is greater than 0. If it is, the player is in the group. If you don’t find the group in the array, that means the player is not in the group.

Though this is more work than just using the methods under the Player object, it provides more up-to-date information as the methods under the Player object cache.


Due to the above method being way more work than it needs to be, I took out time to make an ExtendedGroupService module containing two functions inside it which function similarly to the old methods under the Player object.

If you choose to use the module to make the process quicker, here’s an example of it being used (with the module under ReplicatedStorage, and the Gui inside the server script):

local GroupId = 0 --Your group's ID
local GroupGui = script:WaitForChild("NameOfYourGui")

local PlayersService = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local ExtendedGroupService = require(ReplicatedStorage:WaitForChild("ExtendedGroupService"))

local function PlayerEntered(Player)
	--Check if the player is in the group
	local RankInGroup = ExtendedGroupService:GetUserRankInGroup(Player.UserId, GroupId)
	if RankInGroup > 0 then
		--Player is in group, give them a clone of the GUI
		local PlayerGui = Player:WaitForChild("PlayerGui")
		GroupGui:Clone().Parent = PlayerGui
	end
end

--Run function for new players and players who are already in the game before the script reaches here
PlayersService.PlayerAdded:Connect(PlayerEntered)
local Players = PlayersService:GetPlayers()
for i = 1, #Players do
	PlayerEntered(Players[i])
end

#8

I thought server-sided scripts couldn’t see PlayerGui?


#9

They can see PlayerGui, but any GUI’s cloned into it from StarterGui when the player enters won’t be visible to the server with experimental mode off.