Greetings. I’ve tried to make a script that changes a player’s team when they equip a tool. This script goes inside the tool. It, however, does not seem to work and I’m trying to understand why and how I would make this work.
local tool = script.Parent
local players = game:GetService("Players")
local player = players:GetPlayerFromCharacter(tool.Parent)
tool.Equipped:Connect(function()
player.Team = game.Teams.Outlaws
end)
I also get this error in the output:
Players.cAoSbOx10.Backpack.Handgun.Illegal:6: attempt to index nil with ‘Team’
(Illegal is the name of the script.)
It seems simple enough, but it just doesn’t work. I’m afraid the cause of the problem might be extremely obvious but I can’t think of anything myself. I appreciate any help.
Ok in line 3 you’re indexing tool.Parent which is the backpack not the player character because this script works in the backpack as well. So change that to
local player = players.LocalPlayer
then go to the tool and turn RequiresHandle off.
Congrats the script works now! But, it will only change the team locally, meaning other players will not see that you changed the team. If this is not what you want then you have to use remote events.
Hope this helped
You try to define player before it even equips tool.
local tool = script.Parent
local players = game:GetService("Players")
local player
tool.Equipped:Connect(function()
player = players:GetPlayerFromCharacter(tool.Parent)
player.Team = game.Teams.Outlaws
end)