I want it so that when the player goes through a door, they touch an invisible part and they change teams.
It doesn’t work as it says:
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.
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)
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 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)