Tween Semi-Broken

I tried to tween the view model to achieve some kind of sway. So in this example code is basically what I did: Using a while loop to tween once every period of time. However, the tween did not run.

tool.Equipped:Connect(function()
	while true do --swaySize is a local number value
		ts:Create(viewModel.Head, TweenInfo.new(swaySize/10),{CFrame = camera.CFrame}):Play()
		wait(swaySize/20)
	end
end)
local ts = game:GetService("TweenService")
local tween = ts:Create(viewModel.Head, TweenInfo.new(swaySize/10), {CFrame = camera.CFrame})

tool.Equipped:Connect(function()
	while true do --swaySize is a local number value
		tween:Play()
		wait(swaySize/20)
	end
end)

It’s best to avoid recreating the same tween both in loops and in functions which are repetitively executed (in this case from a fired “Equipped” event).

2 Likes

This code your using right here might not exactly work because the cframe of the camera is constantly changing and if the tween is created out of the loop the target position would not be in the right place. Btw after I tried ur code there was no difference from the previous one ;-;

That’s a fair assessment to make, I wasn’t sure either which way, if that is the case then a simple change is all that is required.

local ts = game:GetService("TweenService")
local camera
local tween

tool.Equipped:Connect(function()
	camera = workspace.CurrentCamera
	tween = ts:Create(viewModel.Head, TweenInfo.new(swaySize/10), {CFrame = camera.CFrame})
	while true do --swaySize is a local number value
		tween:Play()
		wait(swaySize/20)
	end
end)

I’ve declared camera & tween as nonlocals in-case their assigned values are required elsewhere.

After I ran this thing it still had the same outcome as before. Its possible that I’ve tweened it the wrong way. Before I added the tween service the code was:

local camera = workspace.currentcamera
game:GetService("RenderStepped"):Connect(function()
   viewModel.Head.CFrame = camera.CFrame
end)

and this is what it’s supposed to be like

this is after the tween:

that floating piece of arm is the view model

viewModel.Head.CFrame = camera.CFrame
ts:Create(viewModel.Head, TweenInfo.new(swaySize/10), {CFrame = camera.CFrame})

You’re moving the head to the camera’s CFrame inside a RenderStepped & attempting to tween the head to the camera’s CFrame whenever the tool is activated (in a loop).

I did another test which is making the subject to camera and object to a part in the workspace:

rs.RenderStepped:Connect(function()
camera.CameraType = "Scriptable"
	ts:Create(camera, TweenInfo.new(10), {CFrame = workspace.B.CFrame}):Play()
	wait(10)
end)

I thought its normal at first, but the thing really funny is that IT WORKED. So this tells me that the tween actually does play but some other things are causing it to not work.

I am not sure what did you want to achieve, but here’s a quick example of a viewmodel that I made.

viewmodel.rbxl (33.3 KB)

Video

Um, I have no idea how and why, but after a few rounds of testing(only changing the subject and object of the tween) and it just… worked???

local tween = ts:Create(viewModel.Head, TweenInfo.new(10), {CFrame = goal})
	if equipped == true then
		tween:Play()
		wait(10)
	end

Yeah… maybe, just maybe its some kind of bug. Good for me