Script unable to change teams

The purpose of this script is to change the team of the player to the Players team if they are part of the Winners team and teleport them back to spawn after touching the “BecomePlayerButton”. The script is located in ServerScriptService

local GameWorkspace = game:GetService("Workspace")
local MainSpawn = GameWorkspace.LobbyArea.SpawnLocation
local BecomePlayerButton = GameWorkspace.LobbyArea.WinnersHut.BecomePlayer

BecomePlayerButton.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild("HumanoidRootPart") then
		local playername = hit.Parent.Name
		local player = game.Players:FindFirstChild(playername)
		local rootpart = hit.Parent:FindFirstChild("HumanoidRootPart")
		print(player.Name)
		if player.Team == game.Teams.Winners then
			print("working")
			player.Team = game.Teams.Players
			rootpart.Position = Vector3.new(MainSpawn.Position.X, MainSpawn.Position.Y, MainSpawn.Position.Z)
		end
	end
end)

I believe the problem I am having with this script is that the game seems to still think the player is in the Players team after their team has already been swapped to the Winners team. The script is also unable to change the team of the player.

if player.Team == game.Teams.Players then

When using this if statement the script partially works and teleports the player even if the player is in the Winners team. I think this may be a problem with the script not checking the current value of the player’s team but I am unsure of how to do that. I would greatly appreciate if anyone knows why this is or if they could link a similar devforum post that solves this problem.

1 Like

You’re not using very good code practices in this script. This is going to give you problems in the future.

For example, you should always be using :GetPlayerFromCharacter() instead of findfirstchild.

And I recommend you check for the humanoid, not the rootpart.

2 Likes

Good point. Great information for @pikapikapiiii.

2 Likes

Make sure none of the teams have the same colour. This will make the script not work.

local GameWorkspace = game:GetService("Workspace")
local MainSpawn = GameWorkspace.LobbyArea.SpawnLocation
local BecomePlayerButton = GameWorkspace.LobbyArea.WinnersHut.BecomePlayer

BecomePlayerButton.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") then
		local player = game:GetService("Players"):GetPlayerFromCharacter(hit.Parent)
		local rootpart = hit.Parent:FindFirstChild("HumanoidRootPart")
		if player.Team == game:GetService("Teams").Winners then
			player.Team = game:GetService("Teams").Players
			rootpart.Position = Vector3.new(MainSpawn.Position.X, MainSpawn.Position.Y + 2, MainSpawn.Position.Z)
		end
	end
end)

The same thing applies to your script you made. I just made your script better, use it if you wish.

2 Likes

Thanks so much! The script works now. I’m very grateful for your suggestions.

1 Like

I’ll make sure to take your advice into account for the future. Thanks!

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