A question about how to detect changes on CFrame

how can i use .Heartbeat to detect part CFrame changing so i can remove it

Are you looking for all parts or just some? Cause one way would be to store it’s previous value and compare it.

1 Like

No only 1 part and i am trying to get its cframe changed when bubble is created
and i didnt make a script because i dont know what to do first

Okay. Then something like this should work:

-- Server code
local part = <your part>
local prevCFrame = part.CFrame
local detectChange = game:GetService("RunService").HeartBeat:Connect(function()
	if part.CFrame ~= prevCFrame then
		part:Destroy()
		detectChange:Disconnect() -- I have not used this before, but I think this is correct.
	end
end)
1 Like

thank you it worked but i removed that local on detectChange because detectChange:Disconnect() cannot see it when i ran
for some reason

Oh okay. Might’ve been the case that I needed to do this:

local detectChange
detectChange = game:GetService("RunService").HeartBeat:Connect(function()
	if part.CFrame ~= prevCFrame then
		part:Destroy()
		detectChange:Disconnect() -- Destroy this loop once done
	end
end)

I would look into :Disconnect() more though, a good way to save resources.

detectChange wouldn’t be in scope here. That’s because the connected lambda function is defined before the assignment expression is carried out.

local test = function()
	print(test) --nil
end

test()
local test
test = function()
	print(test) --function: {address in memory}
end

test()

Regarding the thread’s question, you could use the following.

local run = game:GetService("RunService")
local part = script.Parent
local currentPosition = part.CFrame

local connection
connection = run.Heartbeat:Connect(function()
	if currentPosition ~= part.CFrame then
		part:Destroy()
		currentPosition = nil
		connection:Disconnect()
		connection = nil
	end
end)