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