Am I Using AncestryChanged Correctly?

I’m trying to make some blocks move so the player can dodge them while standing on platforms:
image

-- This block of code clones one of the blocks that is supposed to move,
-- and clones the "BlockMover" script into the cloned part. I think it works correctly.
blockOne = game.Workspace.Blocks.Block1
blockTwo = game.Workspace.Blocks.Block2
blockThree = game.Workspace.Blocks.Block3
blockFour = game.Workspace.Blocks.Block4

local function cloneBlock(block)
	local clonedBlock = block:Clone()
	local clonedMoverScript = game.ServerScriptService.BlockController.BlockMover:Clone()
	clonedBlock.Parent = game.Workspace.Blocks.ClonedBlocks
	clonedMoverScript.Parent = clonedBlock
end
-- For quick testing:
wait(5)
cloneBlock(blockOne)

The “BlockMover” script is shown below:

-- This should run when the script is cloned into a block, since its parent is changed.
-- Doesn't print the line out...
script.Parent.AncestryChanged:Connect(function(_, parent)
	print("Ancestry Change Detected")
	local TweenService = game:GetService("TweenService")
	local block = script.Parent
	local Info = TweenInfo.new(5, Enum.EasingStyle.Linear, Enum.EasingDirection.Out, 1, false, 0)
	local Goals = {Position = Vector3.new(78, 6.5, -12)}
	local moveForwards = TweenService:Create(block, Info, Goals)
	moveForwards:Play()
end)

The first script works correctly, and clones the block and scripts into the correct places. The second script does not print the text at all.
Does anyone know why AncestryChanged isn’t firing? And could I be doing this way more efficiently?
Thanks.

Why not just activate the BlockMover script using Disabled when its cloned in and parented?

Honestly I would wrap it in a module that returns a callable function but thats probably way too much for a simple fix.

1 Like

Its my first day programming in Roblox Lua, haven’t researched modules yet. Would Disabled make the script run while it is not in the part yet but just in ServerScriptService?

Ive gotten it to work by changing the BlockMover script to this:

if script.Parent:IsA("BasePart") then
	print("If evaluated to true")
	local TweenService = game:GetService("TweenService")
	local block = script.Parent
	local Info = TweenInfo.new(5, Enum.EasingStyle.Linear, Enum.EasingDirection.Out, 0, false, 0)
	local Goals = {Position = Vector3.new(78, 6.5, -12)}
	local moveForwards = TweenService:Create(block, Info, Goals)
	moveForwards:Play()
end

Forgot that the script would just run again after being cloned, so I only have to check if the parent is a part so it doesn’t run in ServerScriptService right when the game starts.