So basically I’m making a custom hotbar system and it’s not working. Here’s “my” code:
-- The Equipping remote event fires in this function.
-- Items is a table which is `Backpack:GetChildren()`
-- PlayerBackpack is a custom backpack in a `Backpacks` folder in ReplicatedStorage, in which a folder for each player is added named `PlayerUserId_Backpack`
UpdateHotbar();
PlayerBackpack.ChildAdded:Connect(function(Child)
if not table.find(Items, Child) then
table.insert(Items, Child);
UpdateHotbar();
end;
end);
PlayerBackpack.ChildRemoved:Connect(function(Child)
if Child.Parent ~= Character then
table.remove(Items, Items[Child]);
UpdateHotbar();
end;
end);
Character.ChildAdded:Connect(function(Child)
if Child:IsA("Model") and not table.find(Items, Child) then
table.insert(Items, Child);
UpdateHotbar();
end;
end);
Character.ChildRemoved:Connect(function(Child)
if Child.Parent ~= PlayerBackpack then
table.remove(Items, Items[Child]);
UpdateHotbar();
end;
end);
EquipItemRE.OnClientEvent:Connect(function()
for _, Slot in pairs(Hotbar:GetChildren()) do
if Slot:IsA("Frame") then
print(Items[1]);
local Item = Items[tonumber(Slot:WaitForChild("HotkeyNumber").Text)];
if Item.Parent == Character then -- Error in this line.
TweenService:Create(Slot, TweenInfo.new(0.25), {
["Transparency"] = 0.5;
["BackgroundColor3"] = Color3.fromRGB(0, 0, 0);
}):Play();
else
TweenService:Create(Slot, TweenInfo.new(0.25), {
["Transparency"] = 0.8;
["BackgroundColor3"] = Color3.fromRGB(255, 255, 255);
}):Play();
end;
end;
end;
end);
Hi, you should probably be more specific about what is going wrong and what you expected. There is in fact a template for it I think. Anyway, I guess in the line
if Item.Parent == Character then
This probably crashes because item is probably nil, and then trying to get the parent causes an error. Probably you want to check if
tonumber(Slot:WaitForChild("HotkeyNumber").Text
is giving what you expect, by printing it. If it gives 1 and Items[1] gives an item properly, that would be strange. Most likely that’s where it goes wrong, but it’s not part of the script so it’s hard to tell like this.
I’m guessing as you loop over all slots, you try to find an item in Items with a too high number (for example, slot 5 when you only have 4 items in Items), hence Item is nil and looking for the parent crashes the script. Hope this helps!
Well, what do you want the program to do when you have a slot frame for which there is no item?
It seems that it does something for every slot, but then tries to find the item that goes with it, by looking into the Items table, but doesn’t find an item. The value ‘Item’ becomes nil.
Looking for the parent of a nil value crashes the script. You could first check if any item is found:
if Item and (Item.Parent == Character) then
Now, if ‘Item’ is nil then it knows that ‘nil and something’ can never be true, so it doesn’t even check Item.Parent and so doesn’t crash.
This way it will just go to the else part, and tween the transparency of the frame to .8 and the color to white, for any slot frames for which there is no item.
If you expected there to be more items or fewer slot frames then something else is going wrong there. Maybe the client event fires before the item is awarded/replicated to the character/backpack?
Do you need the remote event? Maybe equipping an item by setting the parent from inventory to backpack would also allow you to redraw the color and transparency. In any case, make sure that either the item in a slot is really there, or that it doesn’t crash when a slot is empty, as in the example I gave.