Team selector button won't function

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

Could you send an image of the developer console? (/console in chat or F9)

now i edited the script cuz someone fixed smth:

local ws = workspace
local redSpawns = {ws.Red1, ws.Red2, ws.Red3, ws.Red4}
local plr = game:GetService(“Players”).LocalPlayer

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

1 Like

but randomspawn doesn’t do anything

Is there anything within the output or the developer console when you run the game?

attempt to index nil with position (in the line of “plr.Character:PivotTo(randomspawn.Position) – Move the player”)

local ws = workspace -- Makes the ws variable the workspace
local r1 = ws:WaitForChild('Red1') -- Waits for Red1 within the workspace
local r2 = ws:WaitForChild('Red2') -- Waits for Red2 within the workspace
local r3 = ws:WaitForChild('Red3') -- Waits for Red3 within the workspace
local r4 = ws:WaitForChild('Red4') -- Waits for Red4 within the workspace
local redSpawns = {r1,r2,r3,r4} -- Puts all r# variables into a 'redSpawns' table
local plr = game:GetService('Players').LocalPlayer -- Makes the 'plr' variable the player

local selectRandomSpawn = math.random(1, #redSpawns) -- Makes the 'selectRandomSpawn' variable a random number with the minimum being 1 and the maximum being the amount of tuples within the table redSpawns
local randomSpawn = redSpawns[selectRandomSpawn] -- Finds the spawnlocation within the number provided from the selectRandomSpaw variable
if randomSpawn then -- Checks if the randomSpawn ~= nil
   plr.Character:MoveTo(randomSpawn.Position) -- Moves the character to the spawnLocation's position
end

You may need to change some of the code to fit with your game.

1 Like

uhhh it makes the player (in the lobby team) spawn in the red team spawn it’s if press the red team button the player will be set in red team and spawn into a random red team spawn

plr = game:GetService("Players").LocalPlayer
ws = workspace
red1 = ws.Red1
red2 = ws.Red2
red3 = ws.Red3
red4 = ws.Red4
local ws = workspace -- Makes the ws variable the workspace
local redSpawns = {red1,red2,red3,red4} -- Puts all red# variables into a 'redSpawns' table
local plr = game:GetService('Players').LocalPlayer -- Makes the 'plr' variable the player

local selectRandomSpawn = math.random(1, #redSpawns) -- Makes the 'selectRandomSpawn' variable a random number with the minimum being 1 and the maximum being the amount of tuples within the table redSpawns
local randomSpawn = redSpawns[selectRandomSpawn] -- Finds the spawnlocation within the number provided from the selectRandomSpaw variable

script.Parent.MouseButton1Click:Connect(function()
	plr.Team = game.Teams.Red
	script.Parent.Parent.Visible = false
	print(plr.Name.. "'s Team Is Set To ".. plr.Team.Name)
	task.wait(.2)
	plr.Character:MoveTo(randomSpawn.Position)
	local hum = plr.Character:FindFirstChild("Humanoid")
	hum.StateChanged:Connect(function(_oldState, newState)
		if newState == Enum.HumanoidStateType.Dead then
			plr.Team = game.Teams.Spectators
			print(plr.Name.. "Has Been Eliminated. They Will Be Playing The Next Match While Waiting !")
		elseif plr.Team == game.Teams.Lobby then
			print(plr.Name.. "Has Been Eliminated. They Will Be Playing The Next Match While Waiting !")
			plr.Team = not game.Teams.Spectators
		end
	end)
end)

Does this work? (I had to remove all the numbers so I don’t really know if it’s correct, and I am very tired)

Edit: I do also think that not [VARIABLE] only works with Booleans.

it says in the output “Red1 is not a valid member of workspace” doesn’t work idk why

1 Like
plr = game:GetService("Players").LocalPlayer
ws = workspace
red1 = ws:WaitForChild('Red1')
red2 =ws:WaitForChild('Red2')
red3 = ws:WaitForChild('Red3')
red4 = ws:WaitForChild('Red4')
local ws = workspace -- Makes the ws variable the workspace
local redSpawns = {red1,red2,red3,red4} -- Puts all red# variables into a 'redSpawns' table
local plr = game:GetService('Players').LocalPlayer -- Makes the 'plr' variable the player

local selectRandomSpawn = math.random(1, #redSpawns) -- Makes the 'selectRandomSpawn' variable a random number with the minimum being 1 and the maximum being the amount of tuples within the table redSpawns
local randomSpawn = redSpawns[selectRandomSpawn] -- Finds the spawnlocation within the number provided from the selectRandomSpaw variable

script.Parent.MouseButton1Click:Connect(function()
	plr.Team = game.Teams.Red
	script.Parent.Parent.Visible = false
	print(plr.Name.. "'s Team Is Set To ".. plr.Team.Name)
	task.wait(.2)
	plr.Character:MoveTo(randomSpawn.Position)
	local hum = plr.Character:FindFirstChild("Humanoid")
	hum.StateChanged:Connect(function(_oldState, newState)
		if newState == Enum.HumanoidStateType.Dead then
			plr.Team = game.Teams.Spectators
			print(plr.Name.. "Has Been Eliminated. They Will Be Playing The Next Match While Waiting !")
		elseif plr.Team == game.Teams.Lobby then
			print(plr.Name.. "Has Been Eliminated. They Will Be Playing The Next Match While Waiting !")
			plr.Team = not game.Teams.Spectators
		end
	end)
end)

aaaaaaaand it works :tada: thank you dude

1 Like

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