CFrame not updating in Script

im trying to update the Attachments CFrame after a task.wait on my CFrame increment script but it won’t update and their are no errors

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

thats the line attachment.CFrame = CFrame.new(0, 0, -78.758)

it does not update the CFrame… any fixes?

Shouldn’t you use attachment.WorldCFrame
Idk I’m just stupid, don’t listen to me.

1 Like

It does update, but right after it does, next iteration of the loop starts and sets it to same value as before (as deltaTime should be similar every time).

1 Like

so how do i make it so the delta time thingy pauses for a second so the position gets updated right after the task wait ends

1 Like

im giving a new CFrame to the attachment not updating it , so i dont need to use WorldCFrame

1 Like

What you are currently doing is: moving the attachment a bit to the side, waiting 20 seconds then resetting it and instantly moving it a bit to the side again, so it will always look like its not moving at all. What exactly are you trying to do with it instead?
If you add another wait after resetting it you would see it does change back.

2 Likes

so lets just say the attachment is a straight line and here 0.1 in (0, 0, deltaTime * 0.1 is the increment per second thats increasing the lines CFrame every second , so what i want to do is after 20 seconds i want the attachment ( straight line ) to go back from where it started from and loop the increment

1 Like

Currently your code just moves it once and waits 20 seconds before returning it back to start. If you just want to move it by 0.1 every second you can just do it like this:

for i=1,20 do --loop 20 times
    attachment.WorldCFrame += Vector3.new(0, 0, 0.1) --move it by 0.1
    task.wait(1) --wait 1 second
end

attachment.CFrame = CFrame.new(0, 0, -78.758) --reset to original position
2 Likes

Im surprised that actually worked but after it returned once it did not increment 0.1 again , can u make it so it loops like that forever? Also i used the delta Heartbeat so it smoothly transition every second instead of a sudden jump… can u add delta into it by any chance?

1 Like

Yeah, to loop it forever you just put it in a while loop, and to move it smoothly its a bit different than your original code:

local run = game:GetService("RunService")
local final = 20 --how long you want it to run

while true do --loop forever
	local cumulative = 0
	
	while cumulative < final do --loop for amount of time specified
		cumulative += run.Heartbeat:Wait() --add amount of time between frames

		attachment.CFrame = CFrame.new(0, 0, -78.758 + cumulative * 0.1) --set cframe to current total * 0.1
	end

	attachment.CFrame = CFrame.new(0, 0, -78.758) --reset to original position
end
2 Likes

that worked perfectly! thank you ive been trying so long to find a solution and ur script finally did it… ur a pro scripter :smiley: Also now i have around 3 attachments so will i have to make a seperate script for each one of those attachments or can i change something in this code so it works for the other 3 attachments too?

1 Like

If you want them to all move the same amount you can simply add them all into this same script. You can just copy paste attachment.CFrame and change the name. If you need them to move different amounts or different speed you could change 0.1 into some other number (currently it moves for 20 seconds and every second increments by 0.1 meaning it moves 2 studs, to change the speed or distance simply increase or decrease 0.1).

will do the needful , thank you so much againn :sob: ur the best

1 Like

changed it for all my attachments , works perfectly! just needed a quick help with the last line where it resets to original position , so i realized it looks a bit odd for my game when the Cframe resets so i was wondering if could add a tween service to the CFrame? ( im not even sure if thats possible…) OR so like it starts to increment backwards with an increased speed after it reaches the end s(o that would make it look like a pendulum motion)

If you are doing this on the client (so it doesn’t update on the server and only player sees the changes), you could just use TweenService for all of this, if instead you need animation to play on the server its best to use the script above and just have another loop incrementing the attachments back.

If you play animation on the client:

local ts = game:GetService("TweenService")

local info = TweenInfo.new(
    20, --time it takes for it to finish
    Enum.EasingStyle.Linear, --linear makes it move at the same speed entire time
    Enum.EasingDirection.Out, --easing direction of the tween
    -1, --how many times to repeat (-1 makes it repeat forever)
    true --if it reverses (when it reaches the end it goes back to start)
)

ts:Create(attachment,info,{CFrame = CFrame.new(0, 0, -76.758)}):Play()

This way however it will take same amount of time to go back and forth.
If you want it to take less time going back you would create 2 tweens:

while true do
    ts:Create(attachment,TweenInfo.new(20),{CFrame = CFrame.new(0, 0, -76.758)}):Play()
    task.wait(20)
    ts:Create(attachment,TweenInfo.new(10),{CFrame = CFrame.new(0, 0, -78.758)}):Play()
    task.wait(10)
end

Keep in mind that tweens dont yield the script, so you need to add a wait after starting the tween.
If you want to animate it on the server instead you would just need to add another while loop to reverse the movement:

cumulative = 0 --reset the total so we can start again

while cumulative < final do --loop for amount of time specified
	cumulative += run.Heartbeat:Wait() --add amount of time between frames

	attachment.CFrame = CFrame.new(0, 0, -76.758 - cumulative * 0.1) --set cframe to current total * 0.1
end

You would just do this instead of resetting it. If you want it to take less amount of time you would just create another final variable, lets say final2 = 10, replace cumulative < final with cumulative < final2 and change 0.1 to 0.2 (in this case 0.2 is enough as 10 * 0.2 = 2, but if you want it to move even faster you would need to calculate it depending on how long you want it to take by doing distance/time distance being 2 studs in this case and time 10 seconds).

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.