I am trying to make a click detector that only gives an item to a player if they are on a certain team. When I attempt to use it, it says that x is a nil value. Originally the script works fine as :
function hi(x)
local y = x.Backpack
local z = game.Lighting["Baseball"]
z:Clone().Parent = y
end
script.Parent.ClickDetector.MouseClick:connect(hi)
However, when I use part of a team door script to try and make the part that gives the item a team only item, it breaks. The modified script that does not function is down below.
function hi(x)
modelname="Baseball Player" -- Put the EXACT name of the team here
if game.Players:playerFromCharacter(x.Parent).TeamColor==game.Teams:findFirstChild(modelname).TeamColor then
local y = x.Backpack
local z = game.Lighting["Baseball"]
z:Clone().Parent = y
end
end
script.Parent.ClickDetector.MouseClick:connect(hi)
Any idea how to make this function?

2 comments
playerFromCharacter is deprecated. Try Players:GetPlayerFromCharacter, Also why are you using TeamColor instead of Team?
For line 3 use this: if x game.Teams.[modelname]:FindFirstChild[x.Name] then -- Replace "Team name here" with your team name