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

Tweening malfunction with no output error?

Please actually read this before trying to answer.

So basically I have this code that tweens an object to a random position every couple seconds. But instead, it just goes to the top left corner and doesn't move again after.

Pic = script.Parent
NextMoveVert = math.random(.25,.35)
NextMoveHorz = math.random(0.45,0.35)
local movetime = 3
while true do
wait (5)	
	Pic:TweenPosition(UDim2.new(NextMoveHorz, 0, NextMoveVert, 0), "InOut", "Quint", movetime)
end




There are no errors at all.

1 answer

✓ Accepted answer

The reason this isn't working as expected is because you only generate a random number once; you want a new random position to be chosen each time. To accomplish this, you need to call math.random() in each iteration.

local Pic = script.Parent --// Usage of local variables is recommended.
local movetime = 3
while wait(5) do --// You can move your wait here and it will work the same way.
	local NextMoveVert = math.random(.25,.35)
	local NextMoveHorz = math.random(0.45,0.35)
	Pic:TweenPosition(UDim2.new(NextMoveHorz, 0, NextMoveVert, 0), "InOut", "Quint", movetime)
end

Hope this helped.

EDIT: In a rush to correct the obvious issue, it seems I missed a few others. First, your range of numbers is small-- .25-.35 and .35-.45. The difference will be small if you choose these numbers.

Secondly, math.random rounds non-integer values to the nearest integer; that is, decimals aren't accepted.

Finally, in your second math.random call, the larger number is before the smaller number, when it should be the other way around. You can fix these issues with a little bit of math (and switching two numbers around):

local Pic = script.Parent --// Usage of local variables is recommended.
local movetime = 3
while wait(5) do --// You can move your wait here and it will work the same way.
	local NextMoveVert = math.random(25, 35)/100;
	local NextMoveHorz = math.random(35, 45)/100;
	Pic:TweenPosition(UDim2.new(NextMoveHorz, 0, NextMoveVert, 0), "InOut", "Quint", movetime)
end

Hope this helped.

2 comments

0

Everyone keeps saying the same answer, but I still get the same problem. Please test your answer before submitting...

0

Sorry about that! I've added an edition.

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