local player = workspace:FindFirstChild("Player") if player.TeamColor==BrickColor.new("Medium stone grey") then print("Player is a civilian, no shot ammo for you!") elseif player.TeamColor==BrickColor.new("Bright red") or player.TeamColor==BrickColor.new("Navy blue") or player.TeamColor==BrickColor.new("Navy blue") then script.Parent.ClickDetector.MouseClick:connect(function(plr) ypcall(function() for _,v in pairs(plr.Character:children()) do if v:IsA("BasePart") then v.Anchored = false end end script.Parent.AmmoGiver:Clone().Parent = plr.PlayerGui end) end) end
11:05:24.855 - Workspace.Limber.Ammo Box.Innards.Clicky.Script:3: attempt to index local 'player' (a nil value)
How do I fix this?
local players = game.Players:GetPlayers() for _, player in pairs(players) do if player.TeamColor==BrickColor.new("Medium stone grey") then print("Player is a civilian, no shot ammo for you!") elseif player.TeamColor==BrickColor.new("Bright red") or player.TeamColor==BrickColor.new("Navy blue") or player.TeamColor==BrickColor.new("Navy blue") then script.Parent.ClickDetector.MouseClick:connect(function(plr) ypcall(function() for _,v in pairs(plr.Character:children()) do if v:IsA("BasePart") then v.Anchored = false end end script.Parent.AmmoGiver:Clone().Parent = plr.PlayerGui end) end) end end
WaitForChild() will wait until the Instance exists while FindFirstChild() searches for this Instance. And if this Instance is nil when the script starts running then the variable will be too.
The new problem now is that yours only gets a single character named "Player". If this Instance is nil then the team will of course not work.
15 days later (I'm lazy) and I got it to work myself!!
script.Parent.ClickDetector.MouseClick:connect(function(plr) if plr.TeamColor==BrickColor.new("Medium stone grey") then return else ypcall(function() for _,v in pairs(plr.Character:children()) do if v:IsA("BasePart") then v.Anchored = false end end script.Parent.AmmoGiver:Clone().Parent = plr.PlayerGui end) end end)