Trying to change teams on touch

Hello, I am trying to make a script make a player change teams on touch, I keep getting errors.

local Debounce = false

script.Parent.Touched:Connect(function(h)
if not Debounce then
Debounce = true
game.Players:GetPlayerFromCharacter(h.Parent).Team = game.Teams.Nintendo
wait(10.0)
Debounce = false
end
end)

local Players = game:GetService("Players")

local Debounce = false
script.Parent.Touched:Connect(function(h)
if not Debounce then
Debounce = true
game.Players:GetPlayerFromCharacter(h.Parent).Team = game.Teams.Nintendo
wait(10.0)
Debounce = false
end
end)

Try adding Players above
What error is it returning?

Does the player change teams when touching the team spawn, or a part?

Because I recall you can simply set a property on spawns that changes your team on touch. Unless, you don’t want to do that?

Workspace.TeamChange.Script:5: attempt to index nil with ‘Team’ - Server - Script:5

1 Like

Define this at the top:

game:GetService("Players").PlayerAdded:Connect(function(Player)

This’ll connect the player that was connected to the roblox game

This would also affect all players as far as I know

Is it possible for you to give us the whole script instead of a small portion of it? And is this written in a serverscript or a localscript?

1 Like

That’s what it is doing to me currently.

That is the whole script I have.

I am still getting the same error. It changes it to all players.

It is written in a server script because LocalScript only let’s you see it.

Hello, make sure the player isn’t nil first, you can do that by just creating a variable for player and then checking it.

I’ve done this and It’s working completly fine for me.

local Debounce = false

script.Parent.Touched:Connect(function(h)
	if not Debounce then
		Debounce = true
		local player = game.Players:GetPlayerFromCharacter(h.Parent)
		if player then
			player.Team = game.Teams.Nintendo
		end
		
		wait(10.0)
		Debounce = false
	end
end)
2 Likes

Hey, I’m new to DevForum and had a question. I would of made a post but that would of got me warned for wrong topic so I didn’t know where or who to ask but how do I post “Builder” next to my name?

You can click on your profile and find the “title” option under preferences, but this isnt relevant at all

1 Like

If its changing to all players then use events.

Yes but play it with 2 players. It will tell you the other player is on a team meanwhile the other player has something different. It’s hard to tell if it is because I am using Beta Studio and it might be broken.

I have already tried that and it does the same.

I fixed it! I reinstalled studio and now it works! Thanks!