Why does my team selector won't work?

i have made a script where you select a team “red” or “blue” but the script won’t work i don’t know how to fix it because it works 1 time and doesn’t work the 2nd time
here is the script:

1. plr = game:GetService("Players").LocalPlayer
2. ws = workspace
3. red1 = ws.Red1
4. red2 = ws.Red2
5. red3 = ws.Red3
6. red4 = ws.Red4

7. script.Parent.MouseButton1Click:Connect(function()
8. 	plr.Team = game.Teams.Red
9. 	script.Parent.Parent.Visible = false
10. 	print(plr.Name.. "'s Team Is Set To ".. plr.Team.Name)
11. 	task.wait(.2)
12. 	plr.Character:MoveTo(red1.Position or red2.Position or red3.Position or red4.Position)
13. 	local hum = plr.Character:FindFirstChild("Humanoid")
14. 	hum.StateChanged:Connect(function(_oldState, newState)
15. 	if newState == Enum.HumanoidStateType.Dead then
16. 			plr.Team = game.Teams.Spectators
17. 			print(plr.Name.. "Has Been Eliminated. They Will Be Playing The Next Match While Waiting !")
18. 	elseif plr.Team == game.Teams.Lobby then
19. 			print(plr.Name.. "Has Been Eliminated. They Will Be Playing The Next Match While Waiting !")
20. 			plr.Team = not game.Teams.Spectators
21. 		end
22. 	end)
23. end)

explanation:

from red1 to red4 all of them are spawnlocations
plr is the player obvously
so basically i used task.wait(.2) for it to set the player team otherwise it won’t
and the line of

plr.Character:MoveTo(red1.Position or red2.Position or red3.Position or red4.Position)

doesn’t work as im trying to make the player spawn randomly into one of the spawnlocations to prevent player stacks

and in line 20 “plr.Team = not game.Teams.Spectators” means if the player has the lobby role and he dies then the spectator role won’t be given to him

1 Like

A quick pointer, add local to each variable when defining them in each script, ie:

local ws = workspace

For your specific problem, I would add all the spawns to a table, then chose one at random:

local redSpawns = {ws.Red1, ws.Red2, ws.Red3, ws.Red4}

Then in place of:

plr.Character:MoveTo(red1.Position or red2.Position or red3.Position or red4.Position)

I would select a random spawn, then spawn the character there:

local selectRandomSpawn = math.random(1, #redSpawns)  -- Pick a number between 1 and the # of Re3d Spawns
local randomSpawn = redSpawns[selectRandomSpawn] -- Get the random Spawn from the table
plr.Character:PivotTo(randomspawn.Position) -- Move the player

I’ve written it “long hand” so it is easier to understand

pointer like where the player will spawn?

No. I was trying to tell you to declare your variables with the local declaration

ahh didn’t do that and thank you ur the g of the day :smiley:

no i do know how to script i just ran into this issue

Sorry. Didn’t mean to insult you

i didn’t mean by an insult you actually saved my game dude you didn’t insult me

but the randomspawn what does it do ? you didn’t write what is randomspawn