How do I change a player's team when it touches an object?

  1. What do you want to achieve?
    I would like to change a player’s team after touching an object and reset their player

  2. What is the issue?
    incompetence

  3. What solutions have you tried so far?
    Well, since I don’t know how to use lua yet, I’ve tried looking at the resources online and typing my own code but to no avail.

my code:



local function onTouched(otherPart)
	
	local partParent = otherPart.Parent
	local humanoid = partParent:FindFirstChildWhichIsA("Humanoid")
	
	if humanoid then
		humanoid.Team = "red";
	end
end```

You can do this:

local function onTouched(otherPart)

	local partParent = otherPart.Parent
	local humanoid = partParent:FindFirstChildWhichIsA("Humanoid")

	if humanoid then
		game.Players:GetPlayerFromCharacter(humanoid.Parent).Team = game.Teams.Red
	end
end

game.Workspace.Part.Touched:Connect(onTouched)

(Edit: Fixed a little mistake)

ok so I’ve put it in but the team still doesn’t change

Can you show us where you place it with a screenshot?

can’t, my upload button is refusing to work today

You can use Snipping Tool to capture screen, then do copy and paste.

The icon of Snipping Tool
image

No I mean my computer won’t let me upload files and idk why I’m on a mac that hasn’t been updated in a couple months.

but my script is in my part

Is it a Server Script or Local Script?
Also, did you change game.Workspace.Part to your desired part?

local function onTouched(otherPart)

	local partParent = otherPart.Parent
	local humanoid = partParent:FindFirstChildWhichIsA("Humanoid")

	if humanoid then
		game.Players:GetPlayerFromCharacter(humanoid.Parent).Team = game.Teams.Red
	end
end

game.Workspace.Part.Touched:Connect(onTouched) -- Here, change game.Workspace.Part to your desired part
1 Like

Thanks! that was the problem, I had used a variable to hold that position with the developer site tutorial and I forgot to put it in.

1 Like