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

How do i show a button only if a player is in a specific Team?

Asked by 3 years ago
Edited 3 years ago

so as i said in the title i want to make a button that only shows if the player is in a specific team and ive already tried this:

while true do
    local Teams = game:WaitForChild("Teams")
    wait(0.01)
    if script.Parent.Parent.Parent.Parent.Team.Name == "Administrative Department" or "Ethics Committee" then
        script.Parent.Visible = true
    else
        script.Parent.Visible = false
    end
end

the structure of it is

StarterGUI
    CodeUI(ScreenGUI)
        TextButton
            UICorner
            WhileLoop(script Obove)
            Press(script Below)
            UI(Frame)

And this is the Press Script

script.Parent.MouseButton1Down:Connect(function()
    if script.Parent.UI.Visible == false then   
        script.Parent.UI.Visible = true
        script.Parent.Text = "Close Alarm Code UI"
    else
        script.Parent.UI.Visible = false
        script.Parent.Text = "Alarm Code UI"
    end
end)

1 answer

Log in to vote
0
Answered by 3 years ago

Most programming languages don't support English grammar, so this isn't doing quite what you expect:

if script.Parent.Parent.Parent.Parent.Team.Name == "Administrative Department" or "Ethics Committee" then

Lua interprets the first half as you intended, but the 2nd half it interprets similar to "If the expression "Ethics Committee" is not false/nil, the whole expression can be true". You'll need to spell out what you want. To save typing, we can save the Team object to a variable before using it:

local Teams = game:GetService("Teams") -- Only need to do this once; use GetService not WaitForChild on things like Teams/Players/etc
local teamLabel = script.Parent.Parent.Parent.Parent:WaitForChild("Team")
while true do
    wait() -- No need to specify 0.01 as wait will always wait for around 0.03 anyway
    if teamLabel.Name == "Administrative Department" or teamLabel.Name == "Ethics Committee" then
        script.Parent.Visible = true
    else
        script.Parent.Visible = false
    end
end

Except I'm going to assume that you meant to use .Text not .Name? In either case, we can rewrite that further to only act when the property changes:

local teamLabel = script.Parent.Parent.Parent.Parent:WaitForChild("Team")
local function update()
    script.Parent.Visible = teamLabel.Text == "Administrative Department" or teamLabel.Text == "Ethics Committee"
end
teamLabel:GetPropertyChangedSignal("Text"):Connect(update)
update()
Ad

Answer this question