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

What can I use instead of player.Team?

I made a code which would teleport the players in their respective spawns (which are just parts with the SpawnLocation decal to decrease suspicion) when the round starts and the code looks like this:

local function TeleportAPlayer(player)
	if player.Team == "Red Team" then
		game:GetService("Players").LocalPlayer.Character.HumanoidRootPart.CFrame = CFrame.new(Vector3.new(61, 1, -11078))
	end
	
	if player.Team == "Green Team" then
		game:GetService("Players").LocalPlayer.Character.HumanoidRootPart.CFrame = CFrame.new(Vector3.new(-61, 1, -11077))
	end
	
	if player.Team == "Blue Team" then
		game:GetService("Players").LocalPlayer.Character.HumanoidRootPart.CFrame = CFrame.new(Vector3.new(61, 1, -11199))
	end
	
	if player.Team == "Yellow Team" then
		game:GetService("Players").LocalPlayer.Character.HumanoidRootPart.CFrame = CFrame.new(Vector3.new(-61, 1, -11199))
	end
end

What can I use instead of player.Team (because it is only a server-side script)?

1 comment

0

oops, forgot, use .Name after the Team since you're using a string

2 answers

✓ Accepted answer

"What can I use instead of player.Team (because it is only a server-side script)?" You are using LocalPlayer which is only usable in a local script (as there will only ever be one local player).

You can simplifly this code a lot by using a table to store the key and value. The index the table using the key to get the CFrame value.

When creating a CFrame with just the x,y,z you do not need to create a Vector3 as there is a constructor for CFrame.new(x,y,z).

Example

local spawns = {
	["Red Team"] = CFrame.new(61, 1, -11078),
	-- ect....
}

print(spawns["Red Team"])

The second issue would be down to using player.Team == "Yellow Team" player.Team is the Team instance and will not be true in any case ie instance == string. To resolve this use the Name of the team or the BrickColor.

Example

local spawns = {
	["Red Team"] = CFrame.new(61, 1, -11078),
	-- ect....
}

local function TeleportAPlayer(player)
	-- you should include the correct validation before running the code
	-- ie does the player have a character, humanoid root part and a team that is in the list

	player.Character.HumanoidRootPart.CFrame = spawns[player.Team.Name]
end

I hope this helps. please comment if you have any other questions.

3 comments

0

But how can I do a validation script like you told me to?

0

You would need to check that player.Character is not nil and that player.Character:FindFirstChild("HumanoidRootPart") is not nil as well

0

spawns[player.Team.Name] will also be nil if the index has no value (not found in the table)

Since this is all server-side, you could call the player from the PlayerAdded Event of the Service Players and continue to use the player.Team

You also don't require all of the the separate if-then statements (use elseif)

If you change the team color from some neutral color, you can use the :GetPropertyChangedSignal() Event to check the player's team and then spawn them

Also, use "SetPrimaryPartCFrame()" since calling the HumanoidRootPart specifically may only move the RootPart and not the whole Character in some instances.

Server Script

game:GetService("Players").PlayerAdded:Connect(function(player)
	player:GetPropertyChangedSignal("Team"):Connect(function()
		if player.Team.Name == "Red Team" then
			player.Character:SetPrimaryPartCFrame(CFrame.new(61, 1, -11078)))
		elseif player.Team.Name == "Green Team" then
			player.Character:SetPrimaryPartCFrame(CFrame.new(-61, 1, -11077)))
		elseif player.Team.Name == "Blue Team" then
			player.Character:SetPrimaryPartCFrame(CFrame.new(61, 1, -11199)))
		elseif player.Team.Name == "Yellow Team" then
			player.Character:SetPrimaryPartCFrame(CFrame.new(-61, 1, -11199)))
		end
	end)
end)

Another option instead of using the CFrame of the part, is to simply use the :LoadCharacter() Function of a Player, but only if the "Part with a SpawnDecal" is an actual Spawn

LoadCharacter Example

game:GetService("Players").PlayerAdded:Connect(function(player)
	player:GetPropertyChangedSignal("Team"):Connect(function()
		player:LoadCharacter()
	end)
end)

In order to use: :GetPropertyChangedSignal("Team") you will need to make sure to change the player's Team via another function.

Edit: Use "player.Team.Name" instead of "player.Team"

4 comments

0

Thank you and no it isn't a classic spawn.

0

1st you should be using a table, 2nd player.Team == "Green Team" will not work. It is an Team instance not a string ugh -1

0

ughhhhhhhhh UGHHHHH

0

But can this fire when an event is fired?

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