I am an early intermediate level scripter, and I am currently trying to make a game for fun, and to practice, I have run into a problem where I am unable to make certain tagged parts unanchor when a specific part touches them, this script has no red underlined sections, or causes any errors, any help would be appreciated!
task.wait()
local Tornado = workspace.Tornado
local CollectionService = game:GetService("CollectionService")
local canbedestroyed = CollectionService:GetTagged("canbedestroyed")
for _,Part in pairs(canbedestroyed) do
Part.Touched:Connect(function(hit)
local Strength = Part:GetAttribute("Strength")
local mph = workspace.torado:GetAttribute("mph")
if hit == Tornado then
if mph > Strength then
Part.Anchored = false
end
end
end)
end
this has a pretty common memory leak in it, remember to disconnect connections on parts when they’re destroyed or not needed anymore:
--pro tip: "for ... in t" is legal and slightly faster than "pairs()"
for _,Part in canbedestroyed do
--this is a type annotation, it helps out with autocomplete and they're really useful
local onTouched:RBXScriptConnection, onDestroying:RBXScriptConnection
onTouched = Part.Touched:Connect(function(hit)
if hit == Tornado then
local Strength = Part:GetAttribute("Strength")
local mph = workspace.torado:GetAttribute("mph")
if mph > Strength then
Part.Anchored = false
onTouched:Disconnect()
onDestroying:Disconnect()
end
end
end)
--:Once() is identical to :Connect(...) except that the connection is disconnected when fired
onDestroying = Part.Destroying:Once(function()
onTouched:Disconnect()
end)
end
apart from that, maybe the loop is being executed before the parts exist so try binding a function to CollectionService:GetInstanceAddedSignal('canbedestroyed')
and GetInstanceRemovedSignal('canbedestroyed')
when no longer needed