So I have an error in my code. I have been trying to give all players a hat when they spawn however I get an error for some reason. I would really appreciate it if you could help me.
I get this error: ServerScriptService.Script:4: attempt to index nil with 'UserId' Script 'ServerScriptService.Script', Line 4
Anyways this is my code:
local players = game:GetService("Players") local serverStorage = game:GetService("ServerStorage") local users = players.LocalPlayer.UserId local isAllowed = function(player, tab) for i, v in pairs(tab) do if player.UserId == v then return true end end return false end players.PlayerAdded:Connect(function(player) player.CharacterAdded:Connect(function(character) local hat = serverStorage:WaitForChild("Combat Helmet") if isAllowed(player, users) then local clone = hat:Clone() clone.Parent = character end end) end)
Simple.
The server can not use LocalPlayer as it is never defined only local scripts can use it
Also if your trying to get all the players you need a table not a userid.
Your code should look something like this:
local players = game:service("Players") -- or :GetService('Players') local serverStorage = game:service("ServerStorage") -- or :GetService('ServerStorage') local users = players:players() -- or :GetPlayers() local isAllowed = function(player, tab) for i, v in pairs(tab) do if player.UserId == v then return true end end return false end players.PlayerAdded:Connect(function(player) player.CharacterAdded:Connect(function(character) local hat = serverStorage:WaitForChild("Combat Helmet") if isAllowed(player, users) then local clone = hat:Clone() clone.Parent = character end end) end)