How do I fix Attempt to index nil with UserID

Hello, if I want to award a player a badge it shows ‘Attempt to index nil with UserId’ I don’t know how to fix it, here is the script:

local Tool = game.Workspace.Tool

Tool.Activated:Connect(function()
    local Player = game.Players.LocalPlayer
	local BadgeService = game:GetService("BadgeService")
	local BadgeId = 2153649086

	BadgeService:AwardBadge(Player.UserId, BadgeId)
end)

Its a normal script and its located inside the StarterGUI.

Any help is appreciated!

You can’t get LocalPlayer in a server (normal) script, try to get the character who is using the tool and get the player from there

Tool.Activated:Connect(function()
    local character = Tool.Parent -- when activated, tool is equipped and in the player's character
local Player = game.Players:GetPlayerFromCharacter(character)
	local BadgeService = game:GetService("BadgeService")
	local BadgeId = 2153649086

	BadgeService:AwardBadge(Player.UserId, BadgeId)
end)

Also putting the script in StarterGui is a bad idea, put it in ServerScriptService or inside the tool

2 Likes

how do I get the character if its not a tool?

Can you elaborate further and give examples of what you want to do? I don’t understand what you mean.

1 Like

you cant use localplayer in server scripts

local Tool = game.Workspace.Tool

Tool.Activated:Connect(function()
    local Player = game:GetService("Players"):GetPlayerFromCharacter(Tool.Parent)
	local BadgeService = game:GetService("BadgeService")
	local BadgeId = 2153649086

	BadgeService:AwardBadge(Player.UserId, BadgeId)
end)
1 Like
 local character = Tool.Parent

you can only do it in with the tool because of Tool.Parent, but now I have the probloem in an other script:

local seat = game.Workspace.Toilet.Seat

seat:GetPropertyChangedSignal("Occupant"):Connect(function()
    local player = game.Players:GetPlayerFromCharacter()
	local BadgeService = game:GetService("BadgeService")
	local BadgeId = 2153751259

	BadgeService:AwardBadge(player.UserId, BadgeId)
end)

how do I get the character here?

1 Like
local seat = game.Workspace.Toilet.Seat

seat:GetPropertyChangedSignal("Occupant"):Connect(function()
     local player = game.Players:GetPlayerFromCharacter(seat.Occupant.Parent)
	local BadgeService = game:GetService("BadgeService")
	local BadgeId = 2153751259

	BadgeService:AwardBadge(player.UserId, BadgeId)
end)
3 Likes

Now there is an error “attempt to index nil with ‘Parent’”

Be sure to check if there is an Occupant:

local seat = game.Workspace.Toilet.Seat

seat:GetPropertyChangedSignal("Occupant"):Connect(function()
if not seat.Occupant then return end -- stop if there is no occupant
     local player = game.Players:GetPlayerFromCharacter(seat.Occupant.Parent)
if not player then return end -- if a npc sat there instead (no player found) then stop
	local BadgeService = game:GetService("BadgeService")
	local BadgeId = 2153751259

	BadgeService:AwardBadge(player.UserId, BadgeId)
end)
2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.