Team Switch Script Issue

Hello, I am writing a team switch script

local replicated = game.ReplicatedStorage
local remote = replicated.Events.ChangeTeam

-- teams brickcolors


local cicolor = 'Cocoa'
local mdcolor = 'Magenta'
local smdcolor = 'Navy blue'
local sqcolor = 'Bright red'
local tscolor = 'Deep orange'

-- teams buttons

local frame = script.Parent

local cibutton = frame.citeam
local mdbutton = frame.mdteam
local smdbutton = frame.smdteam
local sqbutton = frame.sqteam
local tsbutton = frame.tsteam

mdbutton.MouseButton1Click:Connect(function(plr)
	if plr:GetRankInGroup(6057034) >= 3 then
		remote:FireServer(BrickColor.new(mdcolor))
	end
end)
cibutton.MouseButton1Click:Connect(function(plr)
	if plr:GetRankInGroup(6789012) >= 2 then
		remote:FireServer(BrickColor.new(cicolor))
	end
end)
smdbutton.MouseButton1Click:Connect(function(plr)
	if plr:GetRankInGroup(9896154) >= 2 then
		remote:FireServer(BrickColor.new(smdcolor))
	end
end)
sqbutton.MouseButton1Click:Connect(function(plr)
	if plr:GetRankInGroup(10862893) >= 1 then
		remote:FireServer(BrickColor.new(sqcolor))
	end
end)
tsbutton.MouseButton1Click:Connect(function(plr)
	remote:FireServer(BrickColor.new(tscolor))
end)

Output - Attempt to index nil with GetRankInGroup
Many thanks if you help me solving my issue.

does not give the player
to have the player do game.Players.Localplayer

Screen Guis belong only to the client, so you have to use local player

local plr = game:GetService("Players").LocalPlayer

P.S. Because of that, MouseButton1Click doesn’t return the player who clicked it.

1 Like
local players = game:GetService("Players")
local plr = players.LocalPlayer or players.PlayerAdded:Wait()

local replicated = game.ReplicatedStorage
local remote = replicated.Events.ChangeTeam
-- teams brickcolors


local cicolor = 'Cocoa'
local mdcolor = 'Magenta'
local smdcolor = 'Navy blue'
local sqcolor = 'Bright red'
local tscolor = 'Deep orange'

-- teams buttons

local frame = script.Parent

local cibutton = frame.citeam
local mdbutton = frame.mdteam
local smdbutton = frame.smdteam
local sqbutton = frame.sqteam
local tsbutton = frame.tsteam

mdbutton.MouseButton1Click:Connect(function()
	if plr:GetRankInGroup(6057034) >= 3 then
		remote:FireServer(BrickColor.new(mdcolor))
	end
end)
cibutton.MouseButton1Click:Connect(function()
	if plr:GetRankInGroup(6789012) >= 2 then
		remote:FireServer(BrickColor.new(cicolor))
	end
end)
smdbutton.MouseButton1Click:Connect(function()
	if plr:GetRankInGroup(9896154) >= 2 then
		remote:FireServer(BrickColor.new(smdcolor))
	end
end)
sqbutton.MouseButton1Click:Connect(function()
	if plr:GetRankInGroup(10862893) >= 1 then
		remote:FireServer(BrickColor.new(sqcolor))
	end
end)
tsbutton.MouseButton1Click:Connect(function()
	remote:FireServer(BrickColor.new(tscolor))
end)

Why do you need the or players.PlayerAdded:Wait()?
It’s not the same as getting player’s character, it’s always available without waiting for it.

This is just not true, please learn how loading works in Roblox.

I understand that on the client, local scripts are run while the game is still loading, but is it really so unclear that the client (LocalPlayer) has to load itself first so core scripts would work? (core scripts are loaded before local scripts)