Randomizing Tweens

Hello! I’m just wondering if it’s possible to randomize tween effects? Instead of one tween, looping over, and over again, could you have a table as an example, with tween effects in it then just randomly pick a tween from that table/multiple?

Yep. Just have an array of Tweens (e.g.

local Tweens = {
    TweenService:Create(...),
 TweenService:Create(...2),
 TweenService:Create(...3),
 TweenService:Create(...4),
}

local function PlayRandomTween()
     Tweens[math.random(1, #Tweens)]:Play()
end

Its simple!

local part = game.Workspace:FindFirstChild("MyPart")

-- Set up a table of possible easing styles
local easingStyles = {
  Enum.EasingStyle.Linear,
  Enum.EasingStyle.Sine,
  Enum.EasingStyle.Quad,
  Enum.EasingStyle.Cubic,
  Enum.EasingStyle.Quart,
  Enum.EasingStyle.Quint
}

-- Set up the TweenInfo object with random values
local tweenInfo = TweenInfo.new(
  math.random(1, 5), -- duration (1-5 seconds)
  easingStyles[math.random(1, #easingStyles)], -- random easing style
  Enum.EasingDirection.Out, -- easing direction
  math.random(0, 10), -- repeat count (0-10)
  true, -- rewind
  math.random() -- delay (0-1 seconds)
)

-- Set up the tween
local tween = TweenService:Create(part, tweenInfo, {
  Size = Vector3.new(1, 2, 3) -- target size
})

-- Play the tween
tween:Play()

Here is a quick example

local tweenService = game:GetService("TweenService")

local object = -- Set to the opject u want to change

local easingStyles = Enum.EasingStyle:GetEnumItems()
local easingDirections = Enum.EasingDirection:GetEnumItems()
local minTime, maxTime = 1, 10
local minRepeatCount, maxRepeatCount = 1, 5
local rewindOptions = {true, false} -- Don't change
local minDelay, maxDelay = 1, 2

local function _randomTween()
	local tweenInfo = TweenInfo.new(
		math.random(minTime, maxTime),
		easingStyles[math.random(1, #easingStyles)],
		easingDirections[math.random(1, #easingDirections)],
		math.random(minRepeatCount, maxRepeatCount),
		rewindOptions[math.random(1, 2)],
		math.random(minDelay, maxDelay)
	)

	local tween = tweenService:Create(object, tweenInfo, {
		-- properties to tween use the format of "propertyName = endingValue,"
	})

	tween:Play()
	tween.Completed:Wait()
end

Thanks for all of the examples! I’ll be trying them later as it’s quite late, Thanks for the responses again, I wish I could set multiple ‘Solutions’, but for the sake of the post I’ll choose one and call it a day :melting_face:

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.