Trouble trying to setup a basic system in my game

So i’ve been trying to make a system for my game where if you have a badge it replaces 2 different ui buttons with different ui buttons however whatever i try it doesnt seem to work,i got tired of it and copied my old code and updated it with AI but still doesnt seem to work,anybody knows why? (im still pretty new at scripting so ai might have made it even more of a mess lol)

local Players = game:GetService("Players")
local BadgeService = game:GetService("BadgeService")

local player = Players.LocalPlayer
local badgeId = 2154351906500 -- Replace with your actual Badge ID

-- Get references to all UI elements
local teleportUI = game:GetService("StarterGui"):WaitForChild("Teleport"):WaitForChild("TeleportFrame")
local uiToShow1 = teleportUI:WaitForChild('BafflingCave') -- First UI to show when player has badge
local uiToShow2 = teleportUI:WaitForChild('CaveView') -- Second UI to show when player has badge
local uiToHide1 = teleportUI:WaitForChild('UnknownCave') -- First UI to hide when player has badge
local uiToHide2 = teleportUI:WaitForChild('UnknownCaveView') -- Second UI to hide when player has badge

-- Set initial visibility (hide badge-locked UIs, show default UIs)
uiToShow1.Visible = false
uiToShow2.Visible = false
uiToHide1.Visible = true
uiToHide2.Visible = true

-- Function to check if the player owns the badge
local function checkBadge()
	local success, hasBadge = pcall(function()
		return BadgeService:UserHasBadgeAsync(player.UserId, badgeId)
	end)

	if success and hasBadge then
		-- Player has badge - show badge UIs and hide default ones
		uiToShow1.Visible = true
		uiToShow2.Visible = true
		uiToHide1.Visible = false
		uiToHide2.Visible = false
	else
		-- Player doesn't have badge - show default UIs and hide badge ones
		uiToShow1.Visible = false
		uiToShow2.Visible = false
		uiToHide1.Visible = true
		uiToHide2.Visible = true
	end
end

-- Run check when the script starts
checkBadge()
1 Like

StarterGui is only used in Studio. You should use PlayerGui

1 Like

already tried to but then i cant connect the teleportframe with it for some reason?

1 Like

Please show me the updated script

1 Like

local Players = game:GetService(“Players”)
local BadgeService = game:GetService(“BadgeService”)

local player = Players.LocalPlayer
local badgeId = 12345678 – Replace with your actual badge ID

– Get PlayerGui, NOT StarterGui
local screenGui = player:WaitForChild(“PlayerGui”):WaitForChild(“Teleport”) – Replace with your GUI name
local teleportFrame = screenGui:WaitForChild(“TeleportFrame”) – Ensure this exists

– Get buttons safely
local button1 = teleportFrame:FindFirstChild(“UnknownCave”)
local button2 = teleportFrame:FindFirstChild(“UnknownCaveView”)
local button3 = teleportFrame:FindFirstChild(“BafflingCave”)
local button4 = teleportFrame:FindFirstChild(“CaveView”)

– Ensure buttons exist before modifying them
if button1 and button2 and button3 and button4 then
local function checkBadge()
local success, hasBadge = pcall(function()
return BadgeService:UserHasBadgeAsync(player.UserId, badgeId)
end)

    if success and hasBadge then
        button1.Visible = false
        button2.Visible = false
        button3.Visible = true
        button4.Visible = true
    else
        button1.Visible = true
        button2.Visible = true
        button3.Visible = false
        button4.Visible = false
    end
end

-- Run the check when the game starts
checkBadge()

else
warn(“One or more buttons not found! Check GUI hierarchy.”)
end

again ai script because not sure how playergui works but this one doesnt connect to teleport frame cuz it doesnt connect to screengui at all

2 Likes

Is the script on the client or is it a LocalScript? I’m asking because you can’t access the LocalPlayer on the server

1 Like

your code doesn’t work because you defined the local function inside the if statement, and you called it outside the if statement. this is what the end of your script should look like:

       button4.Visible = true
    else
        button1.Visible = true
        button2.Visible = true
        button3.Visible = false
        button4.Visible = false
    end

    -- Run the check when the game starts
    checkBadge()
end

and when you post a script, put the entire script inside the triple backticks
EDIT: acutally, I’m not sure if this is the issue because the script indenting might be messed up

it is a local script, not sure why it doesnt work

for backticks one i tried but it didnt work for some reason so only some of the code was in that box lol

put ``` before your script and after your script