Task.wait() not working

i made a script to change CFrames of Attachments per second and it works perfectly but when i want it to reset its Cframe after using task.wait, it does not work…

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

its not changing the attachment.Cframes position

Are you sure its the task.wait? Because on the line where you try to set the CFrame of the attachment it is using the cframe of the attachment not WorldCFrame.

oh ur probably right let me try something

I could be mistaken too but it could be the fact you are using += Vector3.new instead of *= CFrame.new

u were absolutely right but even after i used attachment.WorldCFrame *= CFrame.new(0, 0, deltaTime * 0.1) it failed to change the CFrame after task.wait :confused:

What you’re doing is, you’re setting it back to the original CFrame Everytime.
I would suggest you to do something like the following if you want it to constantly move

while true do
	local deltaTime = game:GetService("RunService").Heartbeat:Wait()
	attachment.CFrame += Vector3.new(0, 0, deltaTime * 0.1)
	task.wait(2)
	attachment.CFrame = attachment.CFrame * CFrame.new(0, 0, -78.758)
end

when i use ur script . the delta time isnt working so its incremenenting in a wrong way and its not smooth…

Are You planning to move the Attachment Smoothly across the place? Why not use TweenService?

1 Like

yes smoothly… honestly i dont know how to implement tween service on my attachment CFrame , can u help me script it? please

Something like this should do

local TweenService = game:GetService("TweenService")

local nyc = game.Workspace:WaitForChild("nycbeam")
local attachment = nyc:WaitForChild("Attachment2")
while true do
	local MoveAttachmentTween = TweenService:Create(attachment, TweenInfo.new(2,Enum.EasingStyle.Linear, Enum.EasingDirection.InOut, 0, false), {CFrame = attachment.CFrame * CFrame.new(0, 0, -78.758)})
	MoveAttachmentTween:Play()
	task.wait(2)
end
1 Like

that almost worked but the attachment increment starts to go on an opposite direction , i want it to increase and stop and then repeat the same at a single area only

You can Play with the table that is changing the CFrame inside the tween till you get your desired results

1 Like

okay thank u so much it kinda worked , i just wanna know how to stop it and loop it for the same area

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