Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

"Unable to cast value to Object"?

Hello.

I am trying to create a script to tween a model to move left to right. Them model in workspace is called model2 and has 4 parts. The goal of the script is to loop through the children of the model and tween them all to a certain position indefinitely.


local part = game.Workspace.model2:GetChildren()


function tweenAll(part)
	for i,v in ipairs(part) do
		if v:IsA("BasePart") then
			
			local TweenService = game:GetService("TweenService")
		
			local tweenInfo = TweenInfo.new(2,Enum.EasingStyle.Linear,Enum.EasingDirection.Out,-1,true,0)

			local tween = TweenService:Create(part,tweenInfo, {Position = Vector3.new(-1836.571, -22.494, 80)})
			tween:Play()
end
	end
		end



tweenAll(part)

I have the Unable to cast method to Object error in the output, I don't know fi one of the parameters is wrong.

2 answers

✓ Accepted answer

It's simple, you've use the argument to move the part. So you need to change part to v

local tween = TweenService:Create(v, tweenInfo, {Position = Vector3.new(-1836.571, -22.494, 80)})

This is your full script.

local part = game.Workspace.model2:GetChildren()

function tweenAll(part)
	for i, v in ipairs(part) do 
		if v:IsA("BasePart") then
			local TweenService = game:GetService("TweenService")
	 
			local tweenInfo = TweenInfo.new(2, Enum.EasingStyle.Linear, Enum.EasingDirection.Out, -1, true, 0)

			local tween = TweenService:Create(v, tweenInfo, {Position = Vector3.new(-1836.571, -22.494, 80)})
			tween:Play()
		end
	end
end

tweenAll(part)

If you have any question put it below on commentaries.

1 comment

0

Oh thank you so much! But another problem arose. The script itself works just dandy but I kind of expected the parts in the model to move in a straight line. Instead when I run the script, to get to the Position mentioned above, the parts in the model all move in a different way to get to the Position. Do you know how to make them all move in a straight line?

There's a more reliable way to perform "model tweening". What you can do is set a single part in the model to be the PrimaryPart. The PrimaryPart will determine the location the whole model will go to.

What you can then do is weld all other parts in the model to the PrimaryPart using Weldconstraints and unanchor them.

Lastly you just have to edit your script so you only tween the PrimaryPart to the new position, the rest of parts will automatically follow your PrimaryPart because of the welds.

3 comments

0

Ohh, this might solve my problem with the parts moving in a different direction.

0

Just set a PrimaryPart in your model and move it with :SetPrimaryPartCFrame(CFrame), you doesn't need to weld your model.

0

@NiniBlackJackQc I used to always do that before however over time (depending on how much you move your model) it'll start to create floating point errors and you get gaps in your model. The welding method I described doesn't have this side effect.

This discussion is preserved as it was. New answers, comments, and votes are closed.