Player team won't change

  1. I want it so that when the player goes through a door, they touch an invisible part and they change teams.

  2. It doesn’t work as it says:

Screen Shot 2021-04-19 at 12.50.48 PM

  1. I have tried grabbing the same code from another part but the thing is that it isn’t the same due to the fact that the player clicks a clickdetector but here, the player actually touches a part with their character.

Code:

local debounce = false

script.Parent.Parent.TeamChangePart.Touched:Connect(function(plr)
	if debounce == false then
		debounce = true
		plr.Team = game.Teams["Mail Delivery Team"]
		print(plr.Name.." joined the mail delivery team")
		wait(10)
		debounce = false
	end
end)

Anyone know my possible error? I’m using a normal script and couldn’t use a localscript.

Touched returns a part, not a player

To get the player

local plr = game.Players:GetPlayerFromCharacter(part.Parent)
if plr then
   -- stuff
1 Like

plr Is not a player, it’s a body part. So use game.Players:GetPlayerFromCharacter(part.Parent).

But if it’s not nil.

local debounce = false

script.Parent.Parent.TeamChangePart.Touched:Connect(function(part)
	local player = game.Players:GetPlayerFromCharacter(part.Parent)
	if debounce == false  and player then
		debounce = true
		player.Team = game.Teams["Mail Delivery Team"]
		print(player.Name.." joined the mail delivery team")
		wait(10)
		debounce = false
	end
end)
1 Like

Now it says I joined the team as print works but in reality, I’m still not changing my team.

use GetService and WaitForChild

player.Team = game:GetService('Teams'):WaitForChild("Mail Delivery Team")

I know everyone mentioned the correct answer but I’ll just put my two cents here

local debounce = false
local teams = game:GetService("Teams")
local mailTeam = teams["Mail Delivery Team"]

script.Parent.Parent.TeamChangePart.Touched:Connect(function(hit)
	local plr = game:GetService("Players"):GetPlayerFromCharacter(hit.Parent)
	if debounce or not plr then return end
	debounce = true
	plr.Team = mailTeam
	print(plr.Name.." joined the mail delivery team")
	wait(10)
	debounce = false
end)

Touched returns the part that hit a part, so you get the player from that.

Also one more thing, you want the debounce to only apply to the player.

local debounce = {}


script.Parent.Parent.TeamChangePart.Touched:Connect(function(part)
	local player = game.Players:GetPlayerFromCharacter(part.Parent)
	if debounce[player] == nil or debounce[player] == false  and player then
		debounce[player] = true
		player.Team = game.Teams["Mail Delivery Team"]
		print(player.Name.." joined the mail delivery team")
		wait(10)
		debounce[player] = false
	end
end)

i said apple instead of apply , dont mind please

This worked perfectly! Thanks a lot!

1 Like

I am not sure if debounce is needed for this case.

local players = game:GetService('Players')
local teams = game:GetService('Teams')
local mailDeliveryTeam = teams:WaitForChild('Mail Delivery Team')

script.Parent.Parent.TeamChangePart.Touched:Connect(function(part)
    local player = players:GetPlayerFromCharacter(part.Parent)
    if not player then return end

    -- Only set the team when needed
    if player.Team ~= mailDeliveryTeam then
        plr.Team = mailDeliveryTeam
        print(plr.Name.." joined the mail delivery team")
    end
end)

Also, there are correct answers already. I am bit too late here lol.

You’d want to check if the player exists first before checking the table, also, if you’re done with the debounce, set it to nil, not false, otherwise the table will not clear up unneeded debounces. This is how it would be better to do

local debounce = {}
local teams = game:GetService("Teams")
local mailTeam = teams["Mail Delivery Team"]

script.Parent.Parent.TeamChangePart.Touched:Connect(function(part)
	local player = game.Players:GetPlayerFromCharacter(part.Parent)
	if not player or debounce[player.Name] then return end
	debounce[player.Name] = true
	player.Team = mailTeam
	print(player.Name.." joined the mail delivery team")
	wait(10)
	debounce[player.Name] = nil
end)

Anytime! If you have anymore issues don’t be afraid to make another post!

1 Like