I wanted the player to get an item when owning a badge, but it doesn't seem to work?
local badge = game:GetService("BadgeService") local badgeId = 18987678 local player = game.Players.LocalPlayer local tool = game.ServerStorage:WaitForChild('BloxyCola') if badge:UserHasBadge(player.userId, badgeId) then local newtool = tool:Clone() newtool.Parent = player.Backpack else print("Player doesn't have") end
This is the error
Workspace.Script:6: attempt to index local 'player' (a nil value)
game.Players.PlayerAdded:connect(function(player) player.CharacterAdded:connect(function(character) if badge:UserHasBadge(player.UserId, badgeId) then tool:Clone().Parent = player.Backpack end end) end)
The other answers are wrong. It doesn't matter whether you use "UserId" or "userId". The problem is in the error message "player (a nil value)"
The only time that error would happen is if you're trying to use "LocalPlayer" in a server script. That will only work in a local script.
However, you can only use the UserHasBadge function in a server script. So, let's change things up a bit:
-- Server script -- Badge handler -- Services local Players = game:GetService'Players' local BadgeServ = game:GetService'BadgeService' local Storage = game:GetService'ServerStorage' -- Configurable local Tool = Storage:WaitForChild'BloxyCola' local BadgeId = 18987678 -- Main Player.PlayerAdded:connect(function(NewPlayer) if BadgeServ:UserHasBadge(NewPlayer.UserId, BadgeId) then Tool:Clone().Parent = NewPlayer.StarterGear end end)
Let me know if you have any questions.
Problem Roblox changed player.userId http://wiki.roblox.com/index.php?title=API:Class/Player
Solution
if badge:UserHasBadge(player.UserId, badgeId) then