Im trying to tween an int variable in a script but im getting errors

Hi, i try to tween a value in my script but it gives me the error: “ContextActionService: Unexpected error while invoking callback: Unable to cast value to Object” here is my code

ammount = ammount + ammount
	if canChange == true then
		if ammount > 350 then
			canChange = false
			game:GetService("TweenService"):Create(ammount, TweenInfo.new(0.001), {ammount = 350}):Play()
			wait(0.1)
			canChange = true
		end
		game:GetService("TweenService"):Create(ammount, TweenInfo.new(ammount, Enum.EasingStyle.Linear), {ammount = 0}):Play()
	end

pls dont reply if ur just gonna complain about the one line tweens thanks in advance

1 Like

What are you trying to achieve in this code?

You can only tween instances iirc. The first parameter of Create should be an instance. In this case, since I assume you’re using an IntValue, it should be the the IntValue itself. Secondly, the third parameter of Create should be a property, in this case it would be ‘Value’.

1 Like

it spins a wheel a certain amount and i want it to slowly slow down

You cant tween numeric values within a script, because they have no properties, and aren’t objects. What you can tween is values outside the script if thats what you mean when referring to numeric values.

In my experience a casting error is due to incompatible value types. Another thing to note is the extremely short duration of the first tween.

That tween is redundant, since tweens are only adjusted each frame (1/60th of a second) at the most. Remove that tween and just replace it with Amount = 350 (also dear god please remove the second “m” in “Ammount”, it’s driving me mad)

To actually fix your issue, we need to know the line in which the error is generated. I’m not confident the code you have provided is even relevant, given that it is Context Action Service we’re on about. Surrounding code is also required.

ammount is defined as a value and at this part

he is trying tween a value which is not possible

This is assuming he knows the line or even the code block of error

Ive made a number value that stores it:

amountval = Instance.new("NumberValue",LocalPlayer)
function go(amount)
	amountval.Value = amountval.Value + amount


end
game:GetService("RunService").Heartbeat:Connect(function()
	if canChange == true then
		if amountval.Value > 350 then
			canChange = false
			game:GetService("TweenService"):Create(amountval, TweenInfo.new(0.001), {Value = 350}):Play()
			wait(0.1)
			canChange = true
		end
		local tInfo = TweenInfo.new(10,Enum.EasingStyle.Linear)
		local tween = game:GetService("TweenService"):Create(amountval,tInfo,{Value = 0})
		tween:Play()
	end
	FRGO.AngularVelocity = -amountval.Value
	FLGO.AngularVelocity = amountval.Value
	RRGO.AngularVelocity = -amountval.Value
	RLGO.AngularVelocity = amountval.Value
	-- ^ these are the wheels that the number controls
end)

that does not make any sense, why would you tween something in just 1/1000 seconds

so it doesnt exceed the maximum of 350 so it just sets it back to 350 instantly

why not directly set it instead? iirc tweening takes memory especially if it tweens very often

1 Like