I want to make a custom inventory and when you click 1 the tool is supposed to equip and when you click it again it is supposed to unequip. However, in my script it only equips. Is there any way to fix that?
UserInputService.InputEnded:Connect(function(input)
if input.KeyCode == Enum.KeyCode.One then
for i, v in pairs(plr.Backpack:GetChildren()) do
if v:IsA("Tool") and v.TextureId == script.Parent.Image then
if v.Parent == plr.Backpack then
plr.Character.Humanoid:EquipTool(v)
else
print("done")
plr.Character.Humanoid:UnequipTools(v)
end
end
end
end
end)
When you press 1 you check if there is a specific tool in your backpack, which when equipped is not in your backpack anymore. Instead you should make a reference to the tool outside of the InputEnded and not use the loop, then you can check if that tool is in the backpack or not:
local tool = plr.Backpack:FindFirstChild("your-tool")
if tool.Parent == plr.Backpack then
plr.Character.Humanoid:EquipTool(tool)
else
plr.Character.Humanoid:UnequipTools()
end
I don’t know why it doesn’t even equip the tool now. Is there anything wrong in my script?
local tool
for i, v in plr.Backpack:GetChildren() do
if v:IsA("Tool") and v.TextureId == script.Parent.Image then
tool = v
break
end
end
UserInputService.InputEnded:Connect(function(input)
if input.KeyCode == Enum.KeyCode.One then
if tool.Parent == plr.Backpack then
plr.Character.Humanoid:EquipTool(tool)
else
print("done")
plr.Character.Humanoid:UnequipTools()
end
end
end)
Its possible that the tool isn’t loaded fast enough so the loop doesn’t catch it. To fix that you can also add this after the loop:
local notLoaded
notLoaded = plr.Backpack.ChildAdded:Connect(function(child)
if child:IsA("Tool") and child.TextureId == script.Parent.Image then
tool = child
notLoaded:Disconnect()
end
end)
We are disconnecting it after the proper tool was found to make sure it doesn’t get triggered every time you unequip the tool.
I modified the script like this and it didn’t work. How should I make it exactly?
local tool
for i, v in plr.Backpack:GetChildren() do
if v:IsA("Tool") and v.TextureId == script.Parent.Image then
tool = v
break
end
end
local notLoaded
notLoaded = plr.Backpack.ChildAdded:Connect(function(child)
if child:IsA("Tool") and child.TextureId == script.Parent.Image then
tool = child
notLoaded:Disconnect()
end
end)
print(tool)
UserInputService.InputEnded:Connect(function(input)
if input.KeyCode == Enum.KeyCode.One then
if tool.Parent == plr.Backpack then
plr.Character.Humanoid:EquipTool(tool)
else
print("done")
plr.Character.Humanoid:UnequipTools()
end
end
end)
I actually fixed it myself based on the information you gaved me.
Here is my corrected script.
local tool
plr.Backpack.ChildAdded:Connect(function()
script.Parent.Changed:Connect(function(property)
if property == "Image" then
for i, v in plr.Backpack:GetChildren() do
if v:IsA("Tool") and v.TextureId == script.Parent.Image then
tool = v
break
end
end
end
end)
end)
print(tool)
UserInputService.InputEnded:Connect(function(input)
if input.KeyCode == Enum.KeyCode.One then
if tool.Parent == plr.Backpack then
plr.Character.Humanoid:EquipTool(tool)
else
print("done")
plr.Character.Humanoid:UnequipTools()
end
end
end)
You should not be doing it like that as you are making a new connection every time something is added to the backpack. Based on the code I assume the image is not loaded fast enough instead of the tool, so instead you can check when image changes and then search for the tool similarly to how you are already doing it:
local tool
local function changeTool() --make a function for changing the tool since we are using it on multiple places
for i, v in plr.Backpack:GetChildren() do
if v:IsA("Tool") and v.TextureId == script.Parent.Image then
tool = v
break
end
end
end
if script.Parent.Image ~= "" then
changeTool() --change the tool if image is loaded
end
script.Parent:GetPropertyChangedSignal("Image"):Connect(function()
changeTool() --change the tool when image is changed
end)
This will make sure that whenever you change the image, the tool changes too.
As far as I know tools should be loaded before scripts run so this should be good enough, but if the tool isn’t loaded still you can make some changes to the script.
I know, but I’m specifically pointing to the fact in your script you are making a new connection every time something is added to the backpack which can ruin the performance in your game, so its best to change that!
The one I made does the same thing, but not checking when something is added to the backpack (as that isn’t needed unless you are adding the tools to it later).