Enabling a script by checking the player's team doesn't work

Alright so im trying to make a script in which. If a player’s on a team “Administrator” then a script (exactly a local script) gets enabled but else its disabled, But this script i made doesn’t work. Why?

local player = game.Players.LocalPlayer
local STarterGui = game:GetService("StarterGui")
local ScriptToDisableOrEnable = STarterGui.NightVision
if player.Team.Name == "Administrator" then
	ScriptToDisableOrEnable.Enabled = true
else
	ScriptToDisableOrEnable.Enabled = false
end

I’m confused what your trying to do…

you should check if a player changes teams when they join… As why are you getting starterGui instead of PlayerGui?

Here is what I would have changed:

local player = game.Players.LocalPlayer
local PlayerGui = player.PlayerGui
local ScriptToDisableOrEnable = PlayerGui.NightVision

local function ChangeScriptEnabled() 
   if player.Team.Name == "Administrator" and  ScriptToDisableOrEnable.Enabled == false then
	   ScriptToDisableOrEnable.Enabled = true
   elseif player.Team.Name ~= "Administrator" and  ScriptToDisableOrEnable.Enabled == true then
	   ScriptToDisableOrEnable.Enabled = false
   end
end

ChangeScriptEnabled() 

game.Players.LocalPlayer:GetPropertyChangedSignal("Team"):Connect(function()
	ChangeScriptEnabled() 
end)

Im bad at explaining stuff but the code you’ve sent me works, Thank you!

1 Like

The main Issue is PlayerGui and StarterGui (I think) as well as :GetPropertyChangedSignal() missing

StarterGui is what is replicated into PlayerGui, while PlayerGui is the Gui a player sees. In otherwords what is in StarterGui is replicated, and after replicated, changes in StarterGui don’t take place to the clients (players). So just be aware of this when scripting!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.