tool.Activated not working

So I’m trying to make a local script which tells a gui how many potions the player has remaining, however when the potion is used it doesn’t relay the updated amount of remaining potions.

local Gui = game.Players.LocalPlayer:WaitForChild("PlayerGui")
local Backpack = Gui:WaitForChild("Backpack")
local Hotbar = Backpack:WaitForChild("HotBar")
local Potions = Hotbar.Stats:WaitForChild("Potions")

local Equipped = false

script.Parent.Equipped:Connect(function()
    
    Equipped = true
    
    local Name = Potions:WaitForChild("Name")
    local Doses = script.Parent.Doses.Value
    local Count = Potions:WaitForChild("Count")
    
    Potions.Visible = true
    Name.Text = script.Parent.Name
    Count.Text = Doses

end)

script.Parent.Activated:Connect(function()
    
    print ("running") -- THIS DOESN'T PRINT    
    local Doses = script.Parent.Doses.Value
    local Count = Potions:WaitForChild("Count")

    Count.Text = Doses
    
end)


script.Parent.Unequipped:Connect(function()
        
    
    Equipped = false
    
    Potions.Visible = false
    
end)
2 Likes

script.Parent returns the parent of the script so are you sure the script is in the tool and not the handle?

3 Likes

image

I think so

1 Like

I found a problem, you are trying to index a player’s backpack while looking through the playerGui lol

image

image
You are trying to find a backpack that’s in the player’s playergui. None of the code below will run until the child loads (which it won’t)

Use this code instead:

local Backpack = game.Players.LocalPlayer:WaitForChild("Backpack")
2 Likes

no backpack is the name of the gui

1 Like

The code below still won’t run until the Gui gets cloned to the playerGui, so you should consider using an alternative.

1 Like