I made a script that allows me to select a team via GUI, but when I select the team, I never spawn at the spawnlocation. Even if I reset, I still spawn at the team selection spawn. The only way I've managed to get this to not happen Is if I allow team change on touch, but that clearly isn't ideal. My code is here:

local player = game.Players.LocalPlayer
local clickSound = script.Parent.Parent.Parent.Sounds.ClickSound
local hoverSound = script.Parent.Parent.Parent.Sounds.HoverSound

script.Parent.MouseButton1Click:Connect(function()
	clickSound:Play()
end)

script.Parent.MouseEnter:Connect(function()
	hoverSound.Playing = true
end)

script.Parent.MouseLeave:Connect(function()
	hoverSound.Playing = false
end)

if player:IsInGroup(groupid) then
	function click(mouse)
		script.Parent.Parent.Parent.Parent.Parent.TeamColor = BrickColor.new("Really red")
		wait(0.5)
		script.Parent.Parent.Visible = false
		script.Parent.Parent.Parent.CreditButton.Visible = true
		script.Parent.Parent.Parent.PlayButton.Visible = true
		script.Parent.Parent.Parent.TeamButton.Visible = true
		script.Parent.Parent.Parent.Title.Visible = true
		
	end
else 
	function click(mouse)
		script.Parent.Parent.NotInGroup.Visible = true
		wait(4)
		script.Parent.Parent.NotInGroup.Visible = false
	end
end

script.Parent.MouseButton1Down:Connect(click)

Any help is appreciated. The 'groupid' thing written in IsInGroup is not a mistake, I was just using that to test what happens when you aren't in the group.