Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How can i refresh my inventory script every so many seconds?

So ive got an inventory system, you press a button it opens a page shows ur items ect, but this inventory acts as a hotbar instead, you can open and close it.....problem is if you obtain a new item for example buying a sword, it doesn't show it in the inventory until you refresh it by closing it then reopening it....

so my question is, is there something i can add into my inventory script that refreshes is on a loop every so many seconds?

here is the script so u can see whats up.

wait(3)

player = game.Players.LocalPlayer

game.StarterGui:SetCoreGuiEnabled('Backpack',false)


--
local Enabled = false


function UpdateInventory()
    if Enabled == false then
        Enabled = true
        script.Parent.Frame.Visible = true
        script.Parent.HotBar.Visible = true
        script.Parent.Left.Visible = true
        script.Parent.Right.Visible = true
        script.Parent.MoveBar.Visible = true
        script.Parent.Frame.CanvasSize = UDim2.new(0,48*#player.Backpack:children(),0,0)
        script.Parent.Frame:ClearAllChildren()
        for i,v in pairs(player.Backpack:GetChildren()) do
            Obj1 = Instance.new('ImageButton',script.Parent.Frame)
            Obj1.Size = UDim2.new(0,40,0,40)
            Obj1.Position = UDim2.new(0,45*i-16,0,7)
            Obj1.BackgroundTransparency = 0
            Obj1.Image = v.TextureId
            Obj1.MouseButton1Down:connect(function()
                player.Character.Humanoid:UnequipTools()
                player.Character.Humanoid:EquipTool(v)
            end)
        end
    else
        Enabled = false
        script.Parent.Frame.Visible = false
        script.Parent.HotBar.Visible = false
        script.Parent.Left.Visible = false
        script.Parent.Right.Visible = false
        script.Parent.MoveBar.Visible = false
    end
end

script.Parent.InvenButton.MouseButton1Down:connect(function()
    UpdateInventory()
end)

player.Backpack.Changed:connect(function()
    if Enabled == false then Enabled = true UpdateInventory() end
    if Enabled == true then Enabled = false UpdateInventory() end
end)

1 comment

0

Try this

1 answer

✓ Accepted answer
wait(3)

player = game.Players.LocalPlayer

game.StarterGui:SetCoreGuiEnabled('Backpack',false)
local TIMETOREFRESH =      --How long until inv update

local Enabled = false


function UpdateInventory()
    if Enabled == false then
        Enabled = true
        script.Parent.Frame.Visible = true
        script.Parent.HotBar.Visible = true
        script.Parent.Left.Visible = true
        script.Parent.Right.Visible = true
        script.Parent.MoveBar.Visible = true
        script.Parent.Frame.CanvasSize = UDim2.new(0,48*#player.Backpack:children(),0,0)
        script.Parent.Frame:ClearAllChildren()
        for i,v in pairs(player.Backpack:GetChildren()) do
            Obj1 = Instance.new('ImageButton',script.Parent.Frame)
            Obj1.Size = UDim2.new(0,40,0,40)
            Obj1.Position = UDim2.new(0,45*i-16,0,7)
            Obj1.BackgroundTransparency = 0
            Obj1.Image = v.TextureId
            Obj1.MouseButton1Down:connect(function()
                player.Character.Humanoid:UnequipTools()
                player.Character.Humanoid:EquipTool(v)
            end)
        end
    else
        Enabled = false
        script.Parent.Frame.Visible = false
        script.Parent.HotBar.Visible = false
        script.Parent.Left.Visible = false
        script.Parent.Right.Visible = false
        script.Parent.MoveBar.Visible = false
    end
end

script.Parent.InvenButton.MouseButton1Down:connect(function()
    UpdateInventory()
end)
while wait(TIMETOREFRESH) do
       UpdateInventory()
end
player.Backpack.Changed:connect(function()
    if Enabled == false then Enabled = true UpdateInventory() end
    if Enabled == true then Enabled = false UpdateInventory() end
end)

4 comments

0

I am having issues with studio, completely untested

0

it refreshes it causes it to keep opening and closing, i have the time set to 1 so when somone buys something it would show up in their inventory quickly

0

Seemed like it was going to be a bit of a pain to get this to refresh while still showing the GUI so i just went ahead and added a noticeable refresh button lel SO thanks for the time and help :) 

0

So I did help? Awesome. And you're welcome.

This discussion is preserved as it was. New answers, comments, and votes are closed.