I have a long stretch of code here for a bomb script inside a tool that makes acid, but the acid does not anchor, the acid anchoring code piece is on line 41. Its supposed to make acid for an early prototype of a bomb.
local tool = script.Parent
local char = tool.Parent.Parent.PlayerGui
local acid
tool.Activated:Connect(function()
local part = Instance.new("Part")
part.Parent = workspace
part.Name = "Bomb"
part.Shape = Enum.PartType.Ball
part.Size = Vector3.new(2,2,2)
part.Position = tool.Handle.Position
part.BrickColor = BrickColor.new("Black")
local flashcount = 0
local function flash()
local part = part
part.BrickColor = BrickColor.new("Really red")
wait(0.99)
part.BrickColor = BrickColor.new("Black")
end
while wait(1) do
flash()
flashcount += 1
if flashcount == 4 then
local sound = Instance.new("Sound")
sound.SoundId = "rbxassetid://6823153536"
sound.RollOffMaxDistance = 100
sound.Parent = part
sound:Play()
local explosion = Instance.new("Explosion")
explosion.Parent = workspace
explosion.Position = part.Position
explosion.Hit:Connect(function(parti)
parti.Anchored = false
end)
acid = Instance.new("Part")
acid.Position = part.Position
acid.Parent = workspace
acid.Size = Vector3.new(1, 5, 5)
acid.Anchored = true
break
end
end
wait(4)
part:Destroy()
wait(4)
end)
I also tried isolating this from it but it didn't work. I need to anchor it for an bomb tool in my game.
Your order of code seems to affect the results you actually expected.
When creating instances to-be inserted in the workspace, it is usually done in the order of parenting the instance to workspace last, while applying certain property modifications first. For example, the event listener for the explosion might be registered only after the explosion has gone off, leading to such a thing as never getting that event to be called.
To avoid such a situation, you would move the line
explosion.Parent = part
down to under the event listener.
The same for your acid part, you'd only parent after you've set all the properties, including setting Anchored to true.
What might be happening is, that your explosion blows away the acid part before it even had the chance to get anchored in the first place.
Consider reading the following article to learn more about how Roblox' task schedule prioritizes running your code.