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

How to change the player's team using script?

So, I have been trying to make it work, but it didn't work. This is my code below. Basically, I have a part when the player touches the part. The script inside the part where they change the player's team. It didn't work.

local part = script.Parent
part.Touched:Connect(function(hit)
	if hit.Parent then
		local human = hit.Parent:FindFirstChild("Humanoid")
		if human then
			print("Changed Player's Team")
			SetTeam(human,"LevelTwo")
		end
	end
end)

function SetTeam(player, teamName)
	player.TeamColor = game.Teams[teamName].TeamColor
	if player.Character then
		player.Character:BreakJoints()
	end
end

1 answer

✓ Accepted answer

Well, a player object has a property called team, which can be assigned instead of assigning a team color . One thing you did wrong in your code is that you tried to set the team of the Player's humanoid to that team, not the actual player itself.

With that said

Here is the fixed code(revamped version)

local part = script.Parent
part.Touched:Connect(function(hit)
	local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
	if plr then
		print("Changed Player's Team")
		player.Team = game.Teams.LevelTwo
		hit.Parent:BreakJoints()
	end
end)

In this answer, I used the GetPlayerFromCharcter function of the players service, which returns a player when given a character, or it returns nil if the character parameter isn't a character.

game.Players:GetPlayerFromCharacter(instance Character)


References


Note that the Team property does not replicate across the client-server boundary, therefore it cannot be used in local scripts

3 comments

0

There is an error with IF Statement or at line: 5

0

yea, i sorted that out

0

sorry about that

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