Tween not working - "Unable to cast value to object"

Hi all,
I am once again working on my train simulator and I have another problem, that’s why I’m here.
This time, it’s about TweenSerice, and how it’s throwing errors at me and I don’t know what to do.

The purpose of this function is to calculate the Traction of the train based on the maximum traction and the position of the controller as well as update the BodyThrusts in the bogeys accordingly. But of course, forces, especially bigger ones like on trains, don’t increase/decrease instantly but rather smoothly.

Setting the values the hard way works, however I’m having problems making it smooth. I ruled out using for-loops and therefore had to search for an alternative. I found Tweens and wanted to use them, however I came across an error. The error which is thrown at me is “Unable to cast value to object” and directs me to the line where the tweening takes place.

I have tried various solutions from other threads with similar issues, however none of these helped.
I also tried arranging it differently, without success as well.

Also, if there’s a better alternative, feel free to let me know :slight_smile:

Parts of the script:

function PhysicsHandler(ChangedValue)
	if ChangedValue == "Throttle" then
        -- (irrelevant setup of TractionGoal)
		local ChangedTable = {
			localTraction = 0
		}
		TweenService:Create(ChangedTable, TweenInfo.new(1), {localTraction = TractionGoal})
		-- (irrelevant setting Forces to localTraction)
	end
end

Regards, FloribertHD

You’d probably be getting the error for this reason:
tweenGoal.CFrame = part

That looks good, but it isn’t defining the part’s CFrame, which is why you’re getting the error. to fix this, we do:
tweenGoal.CFrame = part.CFrame

You should then get it working!
(This is just an example, figure out what line this is taking place and check if it’s broken.)
Thanks! : D

2 Likes

Hello,
you are getting this error because you are passing a table as a first argument to TweenService:Create() function, while the first argument required is always an Instance that’s property is targeted to be changed.
So in order to fix this error you’d have to pass in an Instance as a first argument or make different way of achieving your goal.

Hope this helped,
have a nice day!

8 Likes

First of all, thank you so much for your help!
I have now solved the problem by making a NumberValue and assigning the Tween to that, and it works.
So there is really no way around doing this with something outside of the script? Would be nice to do everything inside the script, but it’s also not the end of the world if I had do put another Value into my Values folder.

Also, is it possible to tween at a constant rate? Like so it takes one second to go from 0 to 15 but two seconds to go from 0 to 30? Or do I need to use math again?

1 Like