-- Path: SpicyTool (Local Script)
local tool = script.Parent
local handle = tool:WaitForChild("Handle", 1)
local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")
local SoundService = game:GetService("SoundService")

-- GUI elements
local playerGui = player:WaitForChild("PlayerGui")
local overheatGui = playerGui:WaitForChild("OverheatGui")
local heatFrame = overheatGui:WaitForChild("HeatFrame")
local currentHeat = heatFrame:WaitForChild("CurrentHeat")
local overheatValue = currentHeat:WaitForChild("Overheat")
local overheatFrame = overheatGui:WaitForChild("Overheat")
local shuPerClickGui = playerGui:WaitForChild("SHUPerClick")
local shuPerClickLabel = shuPerClickGui:WaitForChild("TextLabel")
tool:SetAttribute("BaseSHU", 1) -- Set this for base SHU

-- Constants
local CLICKS_TO_OVERHEAT = 15
local BASE_COOLDOWN_TIME = 1
local OVERHEAT_DURATION = 5
local REFILL_DURATION = 0.5
local OVERHEAT_REDUCTION_INTERVAL = 0.5
local OVERHEAT_REDUCTION_AMOUNT = 0.1
local ANIMATION_ID = "rbxassetid://99183644765532"
local LABEL_DISPLAY_TIME = 1
local DOUBLE_SPEED_GAMEPASS_ID = 909100554
local DOUBLE_SHU_GAMEPASS_ID = 909091586 -- New gamepass ID for double SHU

-- Suffix table for number formatting
local suffixTable = {
        "K",  -- Thousand
        "M",  -- Million
        "B",  -- Billion
        "T",  -- Trillion
        "Qa",  -- Quadrillion
        "Qi", 
        "Sx", 
        "Sp", 
        "Oc", 
        "No", 
        "Dc",
        "Ud",
        "Dd",
        "Td",
        "Qad",
        "Qid", 
        "Sxd", 
        "Spd", 
        "Ocd", 
        "Nod", 
        "Vg", 
        "UVg",
        "DVg",
        "TVg",
        "QaVg",
        "QiVg",
        "SxVg",
        "SpVg",
        "OcVg",
        "NoVg",
        "Tg",
        "UTg",
        "DTg",
        "TTg",
        "QaTg",
        "QiTg",
        "SxTg",
        "SpTg",
        "OcTg",
        "NoTg",
        "Qag",
        "Uqg",
        "Dqg",
        "Tqg",
        "Qaqg",
        "Qiqg",
        "Sxqg",
        "Spqg",
        "Ocqg",
        "Noqg",
        "Qng",
        "Uqng",
        "Dqng",
        "Tqng",
        "Qaqng",
        "Qiqng",
        "Sxqng",
        "Spqng",
        "Ocqng",
        "Noqng",
        "Sxg",
        "Usxg",
        "Dsxg",
        "Tsxg",
        "Qasxg",
        "Qisxg",
        "Sxsxg",
        "SpSxg",
        "OcSxg",
        "NoSxg",
        "Spg",
        "Uspg",
        "Dspg",
        "Tspg",
        "Qaspg",
        "Qispg",
        "Sxspg",
        "Spspg",
        "Ocspg",
        "NoSpg",
        "Ocg",
        "Uocg",
        "Docg",
        "Tocg",
        "Qaocg",
        "Qiocg",
        "Sxocg",
        "Spocg",
        "OcOCg",
        "NoOcg",
        "Nog",
        "Unog",
        "Dnog",
        "Tnog",
        "Qanog",
        "Qinog",
        "Sxnog",
        "Spnog",
        "Ocnog",
        "NoNog",
        "C",
}

-- Function to format large numbers with suffixes
local function formatNumber(number)
        local index = 0
        while number >= 1000 and index < #suffixTable do
                number = number / 1000
                index = index + 1
        end

        if index == 0 then
                return tostring(math.floor(number))
        else
                return string.format("%.1f%s", number, suffixTable[index])
        end
end

-- Variables
local clickCount = 0
local lastClickTime = 0
local isOverheating = false
local currentSHUPerClick = 1
local hasDoubleSpeedGamepass = false
local hasDoubleSHUGamepass = false

-- Services
local tweenService = game:GetService("TweenService")
local replicatedStorage = game:GetService("ReplicatedStorage")
local marketplaceService = game:GetService("MarketplaceService")

-- Check if the player owns the gamepasses
local function checkGamepasses()
        local success, hasSpeedPass = pcall(function()
                return marketplaceService:UserOwnsGamePassAsync(player.UserId, DOUBLE_SPEED_GAMEPASS_ID)
        end)
        if success then
                hasDoubleSpeedGamepass = hasSpeedPass
        else
                warn("Failed to check double speed gamepass ownership:", hasSpeedPass)
        end

        local success, hasSHUPass = pcall(function()
                return marketplaceService:UserOwnsGamePassAsync(player.UserId, DOUBLE_SHU_GAMEPASS_ID)
        end)
        if success then
                hasDoubleSHUGamepass = hasSHUPass
        else
                warn("Failed to check double SHU gamepass ownership:", hasSHUPass)
        end
end

-- Call this function when the script loads
checkGamepasses()

-- Function to get the current cooldown time
local function getCurrentCooldownTime()
        return hasDoubleSpeedGamepass and BASE_COOLDOWN_TIME / 2 or BASE_COOLDOWN_TIME
end

-- Create or get the RemoteEvents
local updateSHUEvent = replicatedStorage:FindFirstChild("UpdateSHUEvent")
local updatePetBonusEvent = replicatedStorage:FindFirstChild("UpdatePetBonusEvent")
local toolSwitchEvent = replicatedStorage:FindFirstChild("ToolSwitchEvent")

-- Load the animation
local animationInstance = Instance.new("Animation")
animationInstance.AnimationId = ANIMATION_ID
local animationTrack = humanoid:LoadAnimation(animationInstance)

local function updateSHUPerClick(petBonus, petMultiplier, toolBaseSHU)
        BASE_SHU_PER_CLICK = toolBaseSHU or BASE_SHU_PER_CLICK or 1
        petBonus = petBonus or 0
        petMultiplier = petMultiplier or 1

        -- Apply multiplier to (base SHU + bonus)
        currentSHUPerClick = (BASE_SHU_PER_CLICK + petBonus) * petMultiplier

        -- Apply double SHU gamepass if owned
        if hasDoubleSHUGamepass then
                currentSHUPerClick = currentSHUPerClick * 2
        end

        -- Update the GUI to show the new SHU per click with formatted number
        shuPerClickLabel.Text = string.format("+%s🌶️", formatNumber(currentSHUPerClick))
end

-- Function to show scattered SHU gained with improved animations
local function showScatteredSHUGained()
        -- Clone the template label
        local scatterLabel = shuPerClickLabel:Clone()
        scatterLabel.Parent = shuPerClickGui

        -- Get or create the UIStroke
        local uiStroke = scatterLabel:FindFirstChild("UIStroke")
        if not uiStroke then
                uiStroke = Instance.new("UIStroke")
                uiStroke.Parent = scatterLabel
        end

        scatterLabel.Text = string.format("+%s🌶️", formatNumber(currentSHUPerClick))

        local screenSize = workspace.CurrentCamera.ViewportSize

        -- Set initial properties
        scatterLabel.Size = UDim2.new(0.05, 0, 0.03, 0) -- Start smaller
        scatterLabel.TextTransparency = 0.1 -- Start slightly transparent
        uiStroke.Transparency = 0.1

        -- Calculate random position with some padding
        local padding = 100
        local randomX = math.random(padding, screenSize.X - padding)
        local randomY = math.random(padding, screenSize.Y - padding)
        scatterLabel.Position = UDim2.new(0, randomX, 0, randomY)
        scatterLabel.Visible = true

        -- Create combined animation sequence
        local animationInfo = TweenInfo.new(
                LABEL_DISPLAY_TIME,
                Enum.EasingStyle.Quad,
                Enum.EasingDirection.Out,
                0, -- RepeatCount
                false, -- Reverses
                0 -- DelayTime
        )

        -- Create growing and floating animation
        local growAndFloat = tweenService:Create(
                scatterLabel,
                animationInfo,
                {
                        Size = UDim2.new(0.12, 0, 0.1, 0), -- End larger
                        Position = UDim2.new(0, randomX, 0, randomY - 50), -- Float upward
                        TextTransparency = 1
                }
        )

        -- Create stroke fade animation
        local strokeFade = tweenService:Create(
                uiStroke,
                animationInfo,
                {
                        Transparency = 1
                }
        )

        -- Play animations
        growAndFloat:Play()
        strokeFade:Play()

        -- Cleanup after animation
        growAndFloat.Completed:Connect(function()
                scatterLabel:Destroy()
        end)
end

-- Function to update heat display
local function updateHeatDisplay()
        local heatPercent = math.min(clickCount / CLICKS_TO_OVERHEAT, 1)
        tweenService:Create(currentHeat, TweenInfo.new(0.2, Enum.EasingStyle.Quad, Enum.EasingDirection.Out), 
                {Size = UDim2.new(heatPercent, 0, currentHeat.Size.Y.Scale, 0)}):Play()
end

-- Function to update overheat state display
local function updateOverheatDisplay()
        overheatFrame.Visible = isOverheating
end

-- Function to handle tool activation
local function onActivated()
        local currentTime = tick()
        local currentCooldownTime = getCurrentCooldownTime()

        if currentTime - lastClickTime < (isOverheating and currentCooldownTime/2 or currentCooldownTime) then
                return
        end
        lastClickTime = currentTime

        local success, error = pcall(function()
                animationTrack:Play()
        end)
        if not success then
                warn("Failed to play animation: " .. tostring(error))
        end

        updateSHUEvent:FireServer(currentSHUPerClick)
        SoundService:FindFirstChild("Eat"):Play()
        showScatteredSHUGained()

        if not isOverheating then
                clickCount = clickCount + 1
                updateHeatDisplay()

                if clickCount >= CLICKS_TO_OVERHEAT then
                        overheatValue.Value = true
                        isOverheating = true
                        updateOverheatDisplay()
                        spawn(function()
                                local overheatStartTime = tick()
                                while tick() - overheatStartTime < OVERHEAT_DURATION do
                                        wait(OVERHEAT_REDUCTION_INTERVAL)
                                        clickCount = math.max(0, clickCount - CLICKS_TO_OVERHEAT * OVERHEAT_REDUCTION_AMOUNT)
                                        updateHeatDisplay()
                                end
                                overheatValue.Value = false
                                isOverheating = false
                                updateOverheatDisplay()
                                clickCount = 0
                                updateHeatDisplay()
                        end)
                end
        end
end

-- Connect tool activation to the function
tool.Activated:Connect(onActivated)

-- Function to handle equipping and unequipping
local function onEquipped()
        -- Get the base SHU for this tool
        local toolBaseSHU = tool:GetAttribute("BaseSHU") or BASE_SHU_PER_CLICK
        -- Trigger an update on the server
        toolSwitchEvent:FireServer(toolBaseSHU)
end

local function onUnequipped()
        -- Do nothing when unequipped
end

tool.Equipped:Connect(onEquipped)
tool.Unequipped:Connect(onUnequipped)

-- Connect to the UpdatePetBonusEvent to receive updates about equipped pets
updatePetBonusEvent.OnClientEvent:Connect(function(petBonus, petMultiplier, toolBaseSHU)
        updateSHUPerClick(petBonus, petMultiplier, toolBaseSHU)
end) (a gamepass id named: "949307122" would have the same functionality as x2 SHU. like for example. the player bought only one gamepass. then it would be 2 per SHU. however. with this gamepass. then the player would be getting a 4 SHU per click) (include full script. if possible)