I have a problem with my Gui team join Script how can i fix this?

Hello i was working on a Gui button if you click it and you got the right rank in the group you wil be tped to a spawn of the team and be set automatic in the team it works fine the only problem i have is it wont Set me in the team and just let me stand in the normal team where every player can spawn anyone how to fix this ?

Code:

local requiredRankMinimum = 255 --To get rank IDs, look in your group admin page.

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

function Click(mouse) 
	if plr:GetRankInGroup(8514400) >= requiredRankMinimum then 
		local function onPlayerAdded(player)
			plr.TeamColor = BrickColor.new("Really red") -- TeamColor is Cap sensitive.
		end

		local Background = script.Parent.Parent.Parent

		local Fade = Background.Parent.Fade

		wait(.1)

		Fade.Visible = true
		for i = 1,50 do
			Fade.BackgroundTransparency = Fade.BackgroundTransparency - .05
			wait()
		end

		wait(.3) --Change to time you want the Fade frame appear for.

		Background.Visible = false
		game.ReplicatedStorage.TeamChoosingEvent:FireServer(script.Parent.Name)
		plr.Character:MoveTo(game.Workspace.Founder.Position)

		wait(.01)

		for i = 1,50 do
			Fade.BackgroundTransparency = Fade.BackgroundTransparency + .05
			wait()
		end

		Fade.Visible = false
	end 
end 

script.Parent.MouseButton1Click:connect(Click) 

Is it inside of a client script?

it is inside of a Local script so ye

You have defined the “onPlayerAdded” function, but have not called it. Move that function outside of “function Click(mouse)” and call it.

1 Like

Got Explain On how i need to put it in the code?

local requiredRankMinimum = 255 --To get rank IDs, look in your group admin page.

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

local function onPlayerAdded(player)
	plr.TeamColor = BrickColor.new("Really red") -- TeamColor is Cap sensitive.
end

function Click(mouse) 
	if plr:GetRankInGroup(8514400) >= requiredRankMinimum then 
		onPlayerAdded(plr)
		
		local Background = script.Parent.Parent.Parent

		local Fade = Background.Parent.Fade

		wait(.1)

		Fade.Visible = true
		for i = 1,50 do
			Fade.BackgroundTransparency = Fade.BackgroundTransparency - .05
			wait()
		end

		wait(.3) --Change to time you want the Fade frame appear for.

		Background.Visible = false
		game.ReplicatedStorage.TeamChoosingEvent:FireServer(script.Parent.Name)
		plr.Character:MoveTo(game.Workspace.Founder.Position)

		wait(.01)

		for i = 1,50 do
			Fade.BackgroundTransparency = Fade.BackgroundTransparency + .05
			wait()
		end

		Fade.Visible = false
	end 
end 

script.Parent.MouseButton1Click:connect(Click)

i forget to move the plr.TeamColor = BrickColor.new(“Really red”) To but that was the solution Thanks for the help