I made a script that inserts parts that touch a part called “Range” into a table. When I print out the number of items in the table inside of the touched event, it prints out the number of items, which is 12. However, when I print out the number of items outside of the touched event, it prints out 0.
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local gamestarted = ReplicatedStorage:WaitForChild("GameStarted")
local range=script.Parent.Range
local platforms = {}
gamestarted.Event:Connect(function()
range.Touched:Connect(function(hit)
if hit.Name == "center" then
table.insert(platforms,hit)
end
print(#platforms) -- prints out 12
end)
print(#platforms) -- prints out 0
end)
You’re printing it when the event fires right as you connect the Touched event so probably no parts have touched by that time. However, if you get 0 outputted after 12, then that’s an issue
It’s important to know in what order your code is executing. This doesn’t seem like a bug, rather a misunderstanding of how your code is working. 0 logically should print before 12 unless it’s a subsequent firing of the event which then it’d be problematic.
When the gamestarted BindableEvent is fired, it performs two actions. First it connects a Touched event to the range part, then it prints the number of platforms. Range by this point most likely has not been touched before so the print that executes immediately after connecting the Touched event will show 0 items. When range is touched, only then do you register the platforms into the table which then it will expectedly print 12. table.insert is working here.
In short, you’ve wrongly identified when the outer print runs which is immediately after the Touched event has been connected. Since range has not been touched by the time gamestarted fires there’s no table members and therefore the number of platforms is 0 as expected.
On a side note, just saying that this code is going to cause a massive memory leak. If you’re connecting an event within another event in this manner then you need to make sure that you’re getting rid of that connection somehow, either by destroying the range part or by explicitly disconnecting the connection when it’s no longer needed. I hope you’re doing one of those two.
The explanation is pretty Simple , Events doesn’t yield.
In your code
gamestarted.Event:Connect(function()
range.Touched:Connect(function(hit)
if hit.Name == "center" then
table.insert(platforms,hit)
end
print(#platforms) -- prints out 12
end)
print(#platforms) -- prints out 0
end)
the Range.Touched Part is defined , and the function gets called when the Event fires . Since its a different thread print(#platforms) is gonna print 0 cause the gamestarted event just fired and Range.Touched event was not fired before. This is just how events work.
Also the code is gonna cause memory leak , if the gamestarted Event is fired multiple times Touched event could be defined multiple times . So it would be better if you assign a variable to the connection and disconnect it the next time gamestarted Event fires/