Hello, I’m having a bit of trouble with a script i’m trying to work on.
I’ve tried looking around and it seems nobody has made a similar topic on the devforum.
How would I make it so only a specific named player would be able to view the playerlist on the top right of the screen, but have it disabled for everyone else in the server?
I know it should be pretty simple but i’m unsure if its possible, if so how would I go about it?
local playerWhoSeesList = game.Players.GFink
local allOtherPlayers = game.Players:GetPlayers()
table.remove(allOtherPlayers, table.find(allOtherPlayers, playerWhoSeesList))
for _, player in allOtherPlayers do
togglePlayerListRemote:FireClient(player, false)
end
togglePlayerListRemote:FireClient(playerWhoSeesList, true)
Place a ModuleScript called PlayerListToggle in ServerScriptService with this code:
local PlayerListToggle = {}
local Players = game:GetService("Players")
-- Server -> Client Event
local toggleEvent = Instance.new("RemoteEvent")
toggleEvent .Name = "PlayerListToggle"
toggleEvent .Parent = game.ReplicatedStorage
-- Will show the PlayerList for `player`, and hide for everyone else
function PlayerListToggle.toggle(player: Player)
for _,somePlayer in pairs(Players:GetPlayers() do
-- Inform each client whether to show or hide the list (only show for `player`)
toggleEvent:FireClient(somePlayer, somePlayer == player)
end
end
-- Hide for any new players that join!
Players.PlayerAdded:Connect(function(player)
toggleEvent:FireClient(player, false)
end)
return PlayerListToggle
You then call this function on the server to toggle the player list… as an example, you can place the following code in a Script on the server in ServerScriptService
local Players = game:GetService("Players")
local PlayerListToggle = require(game.Workspace.PlayerListToggle)
-- Toggle for a specific player
PlayerListToggle.toggle(game.Players.GFink)
-- Toggle to the newest player
Players.PlayerAdded:Connect(function(player)
PlayerListToggle.toggle(player)
end
Then on the Client… a LocalScript in StarterPlayer.StarterPlayerScripts called PlayerListToggleReciever
local toggleEvent = game.ReplicatedStorage:WaitForChild("PlayerListToggle")
toggleEvent.OnClientEvent:Connect(function(doShow: boolean)
game.StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.PlayerList, doShow)
end)
This is more fleshed out, doing basically all the work for OP, but one thing I see very often which I would advise you against in this case is that ModuleScripts must always return tables. You can just return anything, including this function by itself:
That way, you aren’t having to redundantly use the word “toggle” twice: PlayerListToggle.toggle(player) → PlayerListToggle(player)
Hope this helps in the future.
If you’re wanting a whole system of enabling/disabling the leaderboard (like you would use within a round system), then those other guys above have already done it above, but if you’re wanting it for admins or such, you can put in this code into a LocalScript parented to StarterPlayer>StarterPlayerScripts
Using UserIDs:
local StarterGUI = game:GetService("StarterGui") -- Service needed for functions
local WhitelistedPlayers = {-1,171607757} -- Put the UserId of any player you want to be able to see the whitelist (-1 is for playtesting in studio, 171607757 is your id)
local LocalPlayer = game.Players.LocalPlayer
if not game:IsLoaded() then -- Waits for game to load (this might be uneeded idk)
game.Loaded:Wait()
end
if not table.find(WhitelistedPlayers,LocalPlayer.UserId) then -- If player isn't in whitelist then . .
game.StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.PlayerList, false) -- . . Disable playerlist
end
Using names (not recommended)
local StarterGUI = game:GetService("StarterGui") -- Service needed for functions
local WhitelistedPlayers = {"Player","firefigher2334"} -- Put the name of any player you want to be able to see the leaderboard. "Player" is for testing and "firefigher2334" is your name
local LocalPlayer = game.Players.LocalPlayer
if not game:IsLoaded() then -- Waits for game to load (this might be uneeded idk)
game.Loaded:Wait()
end
if not table.find(WhitelistedPlayers,LocalPlayer.Name) then -- If player isn't in whitelist then . .
game.StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.PlayerList, false) -- . . Disable playerlist
end