I have a problem with my script -- help

  1. What do you want to achieve? Keep it simple and clear!
    I want to fix the part where starterplayer start for firs time their quest when they reached the minimum progress bar it should show claimbutton no the rewardcomplete frame after clicking the claimbutton it should stay hidden then rewardcomplete frame show up

  2. What is the issue? Include screenshots / videos if possible!
    So basically i keep testing a lot on my account when i join the game i did excatly like the quest
    then when i reached full mininum progress bar its showing rewardcomplete frame instead of claimbutton first

  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    i asked chatgpt for help nothing work
    i try changing the set true or false some of them
    still nothing

local questName = "Kill5TeamMemers"
local questValues = game.Players.LocalPlayer:FindFirstChild(questName)

-- Ensure the quest data is cloned and present under the player
if not questValues then
    -- If the quest data isn't found, try to clone from ReplicatedStorage
    local replicatedStorage = game:GetService("ReplicatedStorage")
    local questDataFolder = replicatedStorage:FindFirstChild("QuestData")
    local kill5TeamMemersFolder = questDataFolder and questDataFolder:FindFirstChild("Kill5TeamMemers")
    local necessaryData = kill5TeamMemersFolder and kill5TeamMemersFolder:FindFirstChild("NecessaryData")

    if necessaryData then
        -- Clone and set the parent of the quest data
        local success, err = pcall(function()
            questValues = necessaryData:Clone()
            questValues.Name = questName
            questValues.Parent = game.Players.LocalPlayer
        end)

        if not success then
            warn("Failed to clone quest data: " .. err)
        end
    else
        warn("NecessaryData object not found in ReplicatedStorage -> QuestData -> Kill5TeamMemers.")
    end
end

if questValues then
    local Needed = questValues:WaitForChild("Needed", 10) -- Add a timeout of 10 seconds
    local Current = questValues:WaitForChild("Current", 10) -- Add a timeout of 10 seconds
    local Completed = questValues:WaitForChild("Completed", 10) -- Add a timeout of 10 seconds

    -- Ensure Completed is initialized to false if it doesn't exist
    if not Completed then
        Completed = Instance.new("BoolValue")
        Completed.Name = "Completed"
        Completed.Value = false  -- Set it to false if the player is starting the quest
        Completed.Parent = questValues
    end

    -- Initialize progress bar and number text
    local function updateProgressBar()
        -- Update the progress bar size based on current and needed values
        script.Parent["background uncomplete"].complete.Size = UDim2.new(Current.Value / Needed.Value, 0, 1, 0)
        script.Parent["background uncomplete"]["number text"].Text = Current.Value .. "/" .. Needed.Value
    end

    -- Update progress bar initially
    updateProgressBar()

    -- Function to handle UI visibility logic
    local function updateUIVisibility()
        -- Case 1: Quest has been completed and claimed
        if Completed.Value then
            script.Parent.ClaimButton.Visible = false  -- Hide ClaimButton after quest completion
            script.Parent["background uncomplete"]["RewardComplete"].Visible = true  -- Show RewardComplete
        -- Case 2: Quest is in progress or needs to be claimed
        elseif Current.Value >= Needed.Value and not Completed.Value then
            script.Parent.ClaimButton.Visible = true  -- Show ClaimButton if quest progress is maxed
            script.Parent["background uncomplete"]["RewardComplete"].Visible = false  -- Hide RewardComplete
        -- Case 3: Quest is not yet started or hasn't reached the required progress
        else
            script.Parent.ClaimButton.Visible = false
            script.Parent["background uncomplete"]["RewardComplete"].Visible = false
        end
    end

    -- Initial call to update UI visibility
    updateUIVisibility()

    -- Update progress and visibility when Current changes
    Current.Changed:Connect(function()
        updateProgressBar()  -- Update the progress bar when current progress changes
        updateUIVisibility()  -- Update UI visibility based on progress
    end)

    -- Update UI visibility when Completed value changes
    Completed.Changed:Connect(function()
        updateUIVisibility()  -- Update UI visibility when quest is completed
        updateProgressBar()  -- Update progress bar
    end)

    -- Handle the player's interaction with the ClaimButton
    script.Parent.ClaimButton.MouseButton1Click:Connect(function()
        if Current.Value >= Needed.Value and not Completed.Value then
            -- Player clicks ClaimButton, claim the reward
            game.ReplicatedStorage.Remotes.QuestClaim:FireServer(questName)
            -- Set the quest as completed
            Completed.Value = true
            -- Hide ClaimButton and show RewardComplete after claiming
            script.Parent.ClaimButton.Visible = false
            script.Parent["background uncomplete"]["RewardComplete"].Visible = true
        end
    end)

    -- Ensure UI is updated properly when the player respawns or joins the game
    game.Players.LocalPlayer.CharacterAdded:Connect(function()
        wait(1) -- Wait for the character to load before checking quest status
        updateProgressBar()  -- Update progress bar
        updateUIVisibility()  -- Update UI visibility based on quest completion
    end)
end