Badge not working

Hello! I am trying to make it so when you have a certain badge in your inventory, it makes it so that the gui appears on the screen. However that is NOT what is happening. Can one of you help me?

local BadgeService = game:GetService("BadgeService")
local BadgeID = game.Workspace.Kazoos.GrayKazoo:GetAttribute("BadgeID")
local plr = game.Players:GetPlayerFromCharacter()
local GUI = script.Parent.GrayKazoo

if BadgeService:UserHasBadgeAsync(plr.UserId, BadgeID) then
	GUI.Visible = true
else
	GUI.Visible = false
end

Local script inside frame

I also tried this as a script in server script service

local BadgeService = game:GetService("BadgeService")
local Players = game:GetService("Players")
local plrgui = Players.PlayerGui

local badgeID = game.Workspace.Kazoos.GrayKazoo:GetAttribute("BadgeID")

local function onPlayerAdded(player)
	-- Check if the player has the badge
	local success, hasBadge = pcall(function()
		return BadgeService:UserHasBadgeAsync(player.UserId, badgeID)
	end)

	-- If there's an error, issue a warning and exit the function
	if not success then
		warn("Error while checking if player has badge!")
		return
	end

	if hasBadge then
		plrgui.Main.Inventory.GrayKazoo.Visible = true
	end
end

-- Connect "PlayerAdded" events to the "onPlayerAdded()" function
Players.PlayerAdded:Connect(onPlayerAdded)

Use variable for local script:

local plr = game.Players.LocalPlayer

You’re not giving any input, it expects Character as input to find the player. GetPlayerFromCharacter(CharacterHere). Also in a localscript to get player just do as @piranca said

I’ve fixed it by the way forgot to say it

Mark yourself or someone who helped as solved, then people can see it’s been solved

maybe try this?

local GUI = script.Parent.Parent.GrayKazoo

Maybe check this out?


He is using an up-to-date function, as seen in the post body.