So for an inventory I'm creating I need it to update the hot bar you know, to do that I'm using a changed event. The problemo is that if you keep using it it'll start to lag because the events keep stacking up. I need to know how to disconnect events.
(Not the entire script, just parts I think you'll need for context)
function update()
print("Prime update")
if script.Parent.Image == "" then
script.Parent.Amount.Visible = false
else
if player.Inventory:FindFirstChild(script.Parent.Item.Value)~=nil and player.Inventory[script.Parent.Item.Value].Value == 0 then
script.Parent.Amount.Visible = false
script.Parent.Image = ""
script.Parent.Item.Value = ""
elseif player.Inventory:FindFirstChild(script.Parent.Item.Value)~=nil then
script.Parent.Amount.Visible = true
script.Parent.Amount.Text = "x"..player.Inventory[script.Parent.Item.Value].Value
if a~=nil then
a = nil
end
local a = player.Inventory[script.Parent.Item.Value].Changed:connect(update)
end
end
end
[Completed fixed version]
local player = script.Parent.Parent.Parent.Parent.Parent.Parent.Parent
local a = nil
function onClicked()
if script.Parent.Image~="" then
if game.Workspace:WaitForChild(player.Name):FindFirstChild(script.Parent.Item.Value)~=nil then
game.Workspace:WaitForChild(player.Name).Humanoid:UnequipTools()
else
game.Workspace:WaitForChild(player.Name).Humanoid:UnequipTools()
game.Workspace:WaitForChild(player.Name).Humanoid:EquipTool(game.ReplicatedStorage.assets.Items[script.Parent.Item.Value]:Clone())
end
local kids = player.Backpack:GetChildren()
for i=1,#kids do
if kids[i].ClassName == "Tool" then
kids[i]:Destroy()
end
end
elseif script.Parent.Image == "" then
game.Workspace:WaitForChild(player.Name).Humanoid:UnequipTools()
end
end
function update()
print("Prime update")
if script.Parent.Image == "" then
script.Parent.Amount.Visible = false
if a~=nil then
a:disconnect()
end
else
if player.Inventory:FindFirstChild(script.Parent.Item.Value)~=nil and player.Inventory[script.Parent.Item.Value].Value == 0 then
script.Parent.Amount.Visible = false
script.Parent.Image = ""
script.Parent.Item.Value = ""
elseif player.Inventory:FindFirstChild(script.Parent.Item.Value)~=nil then
script.Parent.Amount.Visible = true
script.Parent.Amount.Text = "x"..player.Inventory[script.Parent.Item.Value].Value
end
end
end
script.Parent.Item.Changed:connect(function()
if a~=nil then
a:disconnect()
end
if script.Parent.Item.Value~="" then
a = player.Inventory[script.Parent.Item.Value].Changed:connect(update)
end
end)
userInput = game:GetService("UserInputService")
userInput.InputBegan:connect(function(input)
if input.UserInputType == Enum.UserInputType.Keyboard and input.UserInputState == Enum.UserInputState.Begin and input.KeyCode == Enum.KeyCode.One then
onClicked()
end
end)
game.Players.LocalPlayer.CharacterAdded:connect(function(character)
update()
end)
script.Parent.MouseButton1Click:connect(onClicked)
script.Parent.Changed:connect(update)
wait(10)
update()
if player.Inventory:FindFirstChild(script.Parent.Item.Value)~=nil then
script.Parent.Amount.Text = "x"..player.Inventory[script.Parent.Item.Value].Value
else
script.Parent.Amount.Text = ""
end

1 comment
The reason why I'm doing it this way is because the item there changes and I don't want it to go through all 100+ items in my game just to get this one thing. I tried putting a:disconnect() on line 14 but it didn't work. Am I using it incorrectly?