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.
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
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)
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)
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)
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)