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

How to show a group rank on a ScreenGUI?

Asked by 8 years ago

I'm having trouble getting the assigned rank to show on a gui. I need help. I currently have this, and it continues down till i have all the ranks

local groupId = 956931

function onPlayerSpawned(player)
  if player:GetRankInGroup(groupId) == 1 then
    script.Parent.header.TextLabel.Text = "Rank: Trainee"
  end
end

function onPlayerSpawned(player)
  if player:GetRankInGroup(groupId) == 2 then
    script.Parent.header.TextLabel.Text = "Rank: Suspended"
  end
end
0
What's the error? TheHospitalDev 1134 — 8y
0
there is none showing iiCoulroPhobia 10 — 8y

2 answers

Log in to vote
2
Answered by 8 years ago

There are a few problems.

Problem 1

The functions aren't being called, they are just being defined. When a function is defined, it does not run (example #1).

Also, since by default, GUIs load every time a player spawns, your functions are not needed because they will only need to run once (example #2).

As well as that, you don't need to make heaps of different functions (example #3)

Example #1

function printText(text) -- this won't work, because it needs to be called
    print(text)
end

printText("yes") -- prints the text
-- something similar to what you were attempting to do
function playerAdded(plr)
    print(plr.Name.. " has joined the game!")
end

game.Players.PlayerAdded:connect(playerAdded) -- calls the function whenever a player joins

Example #2

-- script inside GUI

local group_id = 0000
local player = game.Players.LocalPlayer -- this only works in a LocalScript
local header = script.Parent:WaitForChild("header") --  so it doesn't try to index something that has not loaded yet

local rank = player:GetRankInGroup(group_id)

if rank == 1 then
    header.Text = "Rank: Trainee"
end

Example #3

function spawned()
    if rank == 1 then
        -- code
    elseif rank == 2 then -- notice the elseif
        -- code
    end
end

Useful link for if statements: https://www.youtube.com/watch?v=7aEa6MDNaCs

Problem #2

You can easily get a player's rank name in a certain group using this function:

player:GetRoleInGroup(group_id) -- returns a string, which you can put in a Text object

Hope I helped! Note that I am in a rush, so there may be some errors or discrepancies here and there.

~TDP

0
Still not working, this is what I have. http://prntscr.com/ckrmq8 iiCoulroPhobia 10 — 8y
Ad
Log in to vote
0
Answered by
Async_io 908 Moderation Voter
8 years ago

This is just a second answer to your latest problem. TDP's answer was amazing.

local group_id = 956931
local player = game.Players.LocalPlayer
local header = script.Parent:WaitForChild('header')
local rank = player:GetRankInGroup(group_id)

if rank == 1 then
    header.TextLabel.Text = 'ranknamehere'
elseif rank == 2 then
    head.TextLabel.Text = 'ranknamehere'
elseif rank == 14 then
    head.TextLabel.Text = 'ranknamehere'
end

The PlayerAdded event you were attempting to use would not work, as the current player would have gone into the game, then the scripts would have loaded. Meaning the player would join, the scripts would load the second after the player joins, which does not trigger the PlayerAdded event. You don't even need that considering that you already had player defined. I may have mistyped something incorrectly, but before you comment 'it doesn't work', please double check to make sure that you can't fix it, and there's nothing in the console about the script. If you can't find the error, then say "It doesn't work", and I will help you to the best of my ability.

Answer this question