Hi guys, please look at this video and pay attention to “Connecting” which prints, but it looks like it connects .ChildAdded but it doesn’t fires when a child gets added to backpack
code;
print('Connecting!')
Player.Backpack.ChildRemoved:Connect(function(child)
print('a')
local find = Player.Character:FindFirstChild(child.Name)
if (find == nil) then
print('a')
for i,v in pairs(Backpack.Slots:GetChildren()) do
if (v:IsA("Frame")) then
if (v.TName.Text == child.Name) then
print('b')
v.BackgroundTransparency = .7;
--v.Visible = false;
v.TName.Text = ''
end
end
end
end;
end)
Player.Backpack.ChildAdded:Connect(function(child)
print('aa')
for i = 1,7 do
local slot = Backpack.Slots:FindFirstChild(i)
if slot then
if (slot.Visible == false and #slot.TName.Text < 1) then
local continue = true
for i,v in pairs(Backpack.Slots:GetChildren()) do
if (v:IsA("Frame")) then
if (v.TName.Text == child.Name) then
continue = false;
end
end
end
if (continue == true) then
slot.TName.Text = child.Name;
--slot.Visible = true;
end
end
end
end
end)
Try printing to see if the connection is connected in the first place. If it is then it may be an internal issue. Or it may be an issue on the client vs. server.
print('Connecting!')
local removed = Player.Backpack.ChildRemoved:Connect(function(child)
print('a')
local find = Player.Character:FindFirstChild(child.Name)
if (find == nil) then
print('a')
for i,v in pairs(Backpack.Slots:GetChildren()) do
if (v:IsA("Frame")) then
if (v.TName.Text == child.Name) then
print('b')
v.BackgroundTransparency = .7;
--v.Visible = false;
v.TName.Text = ''
end
end
end
end;
end)
local added = Player.Backpack.ChildAdded:Connect(function(child)
print('aa')
for i = 1,7 do
local slot = Backpack.Slots:FindFirstChild(i)
if slot then
if (slot.Visible == false and #slot.TName.Text < 1) then
local continue = true
for i,v in pairs(Backpack.Slots:GetChildren()) do
if (v:IsA("Frame")) then
if (v.TName.Text == child.Name) then
continue = false;
end
end
end
if (continue == true) then
slot.TName.Text = child.Name;
--slot.Visible = true;
end
end
end
end
end)
print("Connected Remove:",removed.Connected)
print("Connected Added:",added.Connected)
Here you can see i’m literally CLONING the script and pasting it into the localscript that runs it (same interface and code), as you can see .ChildAdded works in baseplate but not in MY game… what the hell?
There’s a silent bug in your event. They can be sneaky to debug, what I’d recommend doing is putting a breakpoint on the line (click next to the line number) where you print the connection in the event and step through it
If there are any issues with a conditional, stepping through it will make it instantly obvious which condition isn’t resolving as true, or if there is some other code misbehaving.
Ok now press the button called Step Into under the Script tab to step through your code. Since the code is finishing (since there’s no errors), it’s better to visualise the computational flow of your program and not just what the functions do.
Also put it after the function(), statement, it’s currently breaking when you connect the function, not when it runs
Oh apologies, then I will suggest some more ideas.
Once a character dies, the Backpack is removed and a new one will be created – populating
Usually this gets messy in a real game with stuff like player:LoadCharacter() as I have experienced with other help topics especially with GUIs. Sometimes the backpack gets destroyed then readded which destroys the connection.
I would also maybe do this to debug further.
local Player = game.Players.LocalPlayer
Player.ChildAdded:Connect(function(child)
if child.Name == "Backpack" then
print(child, "New backpack added!")
end
end)
ex: I click play and somehow there were two backpacks were created.