Attachment Cframe help Script

Hello, Scripting Support. I once again request your help. I am trying to figure out how to Make an attachments CFrame loop over , i already have a script that increases the Cframe of the attachment everysecond now i just want the Cframe position to go back to the orginal position being looped , this is the script

local attachment = game.Workspace.nycbeam:WaitForChild("Attachment2")

while true do
	local deltaTime = game:GetService("RunService").Heartbeat:Wait()
	attachment.WorldCFrame += Vector3.new(0, 0, deltaTime * 0.1) -- increments the attachment by 1-stud
end

if attachment.WorldCFrame == Vector3(0, 0, -51.498) then
	attachment.CFrame = CFrame.new(0, 0, -55.828) 
end

the only thing which doesn’t work is the if statement where i want it to detect the position and send the attachment back to the orginal position so it keeps increasing

1 Like

Basically, any while loop that isn’t contained within a thread/coroutine will loop on forever and not let any code underneath it run.

So to fix this, you could either move the if statement up above the loop, or you could just wrap the loop in a coroutine/task.spawn callback.

can u modify the code with the task.spawn callback? im bad at scripting (':

local attachment = game.Workspace.nycbeam:WaitForChild("Attachment2")

task.spawn(function()
    while true do
    	local deltaTime = game:GetService("RunService").Heartbeat:Wait()
    	attachment.WorldCFrame += Vector3.new(0, 0, deltaTime * 0.1) -- increments the 
        attachment by 1-stud
    end
end)

if attachment.WorldCFrame == Vector3(0, 0, -51.498) then
	attachment.CFrame = CFrame.new(0, 0, -55.828) 
end
1 Like

on line 11 it says , attempt to call a table value , so how do i exactly check the vector3 position do u have any idea regarding that , i want it to basically change the attachments Cframe back to the orginal when it reaches a specific Cframe

1 Like

Oh sorry I didn’t see that, basically WorldCFrame is a CFrame value so to check the position just add .Position after WorldCFrame:

local attachment = game.Workspace.nycbeam:WaitForChild("Attachment2")

task.spawn(function()
    while true do
    	local deltaTime = game:GetService("RunService").Heartbeat:Wait()
    	attachment.WorldCFrame += Vector3.new(0, 0, deltaTime * 0.1) -- increments the 
        attachment by 1-stud
    end
end)

if attachment.WorldCFrame.Position == Vector3(0, 0, -51.498) then
	attachment.CFrame = CFrame.new(0, 0, -55.828) 
end
1 Like

no problem , apparently it gives the same error at line 11… is there any better way to loop it rather thank checking the Cframes position? i tried using wait() but that doesn’t work either idk why.

Sorry for not seeing a lot, its late so my eyes are giving up :joy:

I see you are missing .new for Vector3 in the if statement

local attachment = game.Workspace.nycbeam:WaitForChild("Attachment2")

task.spawn(function()
    while true do
    	local deltaTime = game:GetService("RunService").Heartbeat:Wait()
    	attachment.WorldCFrame += Vector3.new(0, 0, deltaTime * 0.1) -- increments the 
        attachment by 1-stud
    end
end)

if attachment.WorldCFrame.Position == Vector3.new(0, 0, -51.498) then
	attachment.CFrame = CFrame.new(0, 0, -55.828) 
end

OKAY SO NOW THEIR ARE NO ERRORS but it does not do what its supposed to do which is basically looping the attachments Cframe :sob: :sob: Is there any easy way to loop the attachments CFrame omg im so bad at this im so sorry for the trouble

this one script actually worked , but when i added the task.wait into it , it does not work idk why…

local nyc = game.Workspace.roofbeams:WaitForChild("nycbeam")
local attachment = nyc:WaitForChild("Attachment2")
while true do
        local deltaTime = game:GetService("RunService").Heartbeat:Wait()
	attachment.WorldCFrame += Vector3.new(0, 0, deltaTime * 0.1)
	task.wait(20)
	attachment.CFrame = CFrame.new(0, 0, -78.758)
end

omg i know this post is like super old and im necroing it but i just NEED to fix this. the reason why the if statement doesnt work is that it gets executed only once, for it to be executed constantly you need it in a loop or a runservice. the correct script would look like this

local RunService = game:GetService("RunService")
local attachment = game.Workspace.nycbeam:WaitForChild("Attachment2")

RunService.Heartbeat:Connect(function(deltaTime) 
	attachment.WorldCFrame += Vector3.new(0, 0, deltaTime * 0.1) -- increments the attachment by 1-stud

	if attachment.WorldCFrame == Vector3(0, 0, -51.498) then
		attachment.CFrame = CFrame.new(0, 0, -55.828) 
	end
end)

This automatically puts this code into its own thread because of runservice connection which allows any other code to run after this scope. Again sorry for necroing but reading this thread really made me tweak out a bit.