Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Why is my 'Set Team Spawn Location' erroring?

For some odd reason, the code keeps on erroring, but, I am confused as to why, as it keeps giving the same error over-and-over again, and I am confused by the Error, the Output says 20:54:45.334 - TeamColor is not a valid member of Camera 20:54:45.335 - Script 'Players.Player.PlayerGui.setTeamSpawn', Line 8 - global setSpawn 20:54:45.336 - Script 'Players.Player.PlayerGui.setTeamSpawn', Line 16 20:54:45.339 - Stack End and 20:54:45.334 - TeamColor is not a valid member of Model 20:54:45.335 - Script 'Players.Player.PlayerGui.setTeamSpawn', Line 8 - global setSpawn 20:54:45.336 - Script 'Players.Player.PlayerGui.setTeamSpawn', Line 16 20:54:45.339 - Stack End.

I am not too sure why the code keeps erroring as-is, because, the code does not use the Camera object, and keeps thinking I'm trying to use the Model, thus, my confusion on the code.

This is the code I am using;

repeat wait() until game:GetService("Teams") and game.Players.LocalPlayer.TeamColor --Last resort, since it kept on saying those errors, but, it continues ;-;
local plr = game.Players.LocalPlayer;

function setSpawn()
	for i,v in pairs(game.Workspace:GetChildren()) do
		for i2,v2 in pairs(game:GetService("Teams"):GetChildren()) do
			if (v.TeamColor == plr.TeamColor and v2.TeamColor == plr.TeamColor) then
				plr.TeamColor = plr.TeamColor or v2.TeamColor
				plr.Character:MoveTo(v.Position)
			end
		end
	end
end

setSpawn()

game.Players.LocalPlayer.Changed:connect(function(prop)
	if prop == "Character" then return end --To prevent the 'Model' error from showing up; This did not prevent it :(
	setSpawn()
end)

2 comments

1

Why are you using nested "for" loops? And why are you iterating over "game.Workspace?" On line 7, "TeamColor" will be nonexistent , because "v" is in "game.Workspace, the same place where "Camera" is at.

0

That makes a lot of sense. o_e Also, the purpose of this code is due to a Bug in ROBLOX, well, for me at least; It keeps setting any Person's Spawn Point to a Random team location, or, a random 'SpawnLocation' that is not a specific Player's Team.

1 answer

✓ Accepted answer

You were checking the whole of Workspace, in workspace there is camera, that's why you get the camera error because camera doesn't have a TeamColor property. You should check for spawn locations in workspace not everything! I have also made it so they spawn above the spawn location to prevent players from spawning in the ground sometimes. Here's the final code:

-- No need to wait until the character is loaded because the script fires the function when the character is added.
local plr = game.Players.LocalPlayer

function setSpawn()
    for i,v in pairs(game.Workspace:GetChildren()) do
    	if v:IsA("SpawnLocation") then -- Check for spawn locations not everything!
	        for i2,v2 in pairs(game:GetService("Teams"):GetChildren()) do
	            if (v.TeamColor == plr.TeamColor and v2.TeamColor == plr.TeamColor) then
	                plr.TeamColor = plr.TeamColor or v2.TeamColor
	                plr.Character:MoveTo(Vector3.new(v.Position.X,v.Position.Y+3,v.Position.Z)) -- Few studs above the spawn to prevent spawning in the ground.
	            end
	        end
	    end
    end
end

game.Players.LocalPlayer.CharacterAdded:connect(function() -- When character is added.
    setSpawn()
end)

This discussion is preserved as it was. New answers, comments, and votes are closed.