Tween doesnt work properly

So pretty much whenever someone rolls a legendary or mythic I want the text to the a slightly back and forth rotation however for some reason even when the chosenrank is still legendary or mythic it ends the loop. Also when the PosTween finishes it goes instantly to the neg tween without tweeting

local RollButton = script.Parent.RollButton

local RS = game:GetService("ReplicatedStorage")

local Database = require(RS.Database)

local plr = game.Players.LocalPlayer

local LuckMulti = 10

local LuckPerRolls = 10

local function GenerateRNG()
	
	local totalWeight = 0
	
	for Rank, Data in pairs(Database) do
		
		if plr.leaderstats.Rolls.Value % LuckPerRolls == 0 then
			
			print("Lucky Roll")
			
			if Data.LuckBoostable then
				
				totalWeight = totalWeight + Data.Chance * LuckMulti
				
			else
				
				totalWeight = totalWeight + Data.Chance
				
			end
			
		else
			
			totalWeight = totalWeight + Data.Chance
			
		end
		
	end

	local randomNumber = Random.new():NextNumber(0, totalWeight)
	
	local weightSum = 0

	for Rank, Data in pairs(Database) do
		
		if plr.leaderstats.Rolls.Value % LuckPerRolls == 0 then

			if Data.LuckBoostable then

				weightSum = weightSum + Data.Chance * LuckMulti

			else

				weightSum = weightSum + Data.Chance

			end

		else

			weightSum = weightSum + Data.Chance

		end
		
		if randomNumber <= weightSum then
			
			local RankVal = math.round(Random.new():NextNumber(Database[Rank].ValRange[1],Database[Rank].ValRange[2]) * 100) / 100
			
			return Rank, RankVal
			
		end
		
	end
	
end

local RankRevealFrameHolder = script.Parent.RankRevealFrameHolder

local RankRevealFrameHolderPositions = {
	In = UDim2.new(0, 0,-1, 0);
	Out = UDim2.new(0, 0,0, 0);
};

local db = false

local TS = game:GetService("TweenService")

local TweenStyle = TweenInfo.new(0.3,Enum.EasingStyle.Exponential,Enum.EasingDirection.InOut)

local HighRankUIDesigns = RS.HighRankUIDesigns

local RankDisplay = RankRevealFrameHolder.RankRevealFrame.RankDisplay

local ValueDisplay = RankRevealFrameHolder.RankRevealFrame.ValueDisplay

local RankColors = {
	["Common"] = Color3.fromRGB(97, 255, 89);
	["Uncommon"] = Color3.fromRGB(87, 222, 77);
	["Rare"] = Color3.fromRGB(0, 115, 255); -- old color 0,98,255
	["Epic"] = Color3.fromRGB(185, 0, 185);
	["Legendary"] = Color3.fromRGB(255, 193, 60);
	["Mythic"] = Color3.fromRGB(202, 137, 255);
};

local RankPriorities = {
	["None"] = 0;
	["Common"] = 1;
	["Uncommon"] = 2;
	["Rare"] = 3;
	["Epic"] = 4;
	["Legendary"] = 5;
	["Mythic"] = 6;
}

local RewardPlrRoll = RS.RemoteEvents.RewardPlrRoll

local AddRoll = RS.RemoteEvents.AddRoll

local RolledRankOptionsHolder = RankRevealFrameHolder.RolledRankOptionsHolder

local ChosenRank, ChosenRankVal

local RollButtonNormSize = RollButton.Size

local RollComplete

local function Roll()
	
	db = true
	
	AddRoll:FireServer()
	
	TS:Create(RollButton,TweenStyle,{Size = RollButtonNormSize - UDim2.fromScale(0.0162,0.0023)}):Play()
	
	RollButton.TextColor3 = Color3.fromRGB(205, 205, 205)
	
	TS:Create(RankRevealFrameHolder,TweenStyle,{Position = RankRevealFrameHolderPositions.Out}):Play()
	
	local Rolls = 0
	
	repeat
		
		Rolls += 1
		
		ChosenRank, ChosenRankVal = GenerateRNG()
		
		RankRevealFrameHolder.RankRevealFrame.BorderLine.Color = RankColors[ChosenRank]
		
		RankDisplay.Text = ChosenRank

		ValueDisplay.Text = tostring(ChosenRankVal)
		
		if ChosenRank == "Legendary" or ChosenRank == "Mythic" then
			
			RankDisplay.TextColor3 = Color3.fromRGB(255,255,255)
			
			if RankDisplay:FindFirstChildWhichIsA("UIGradient") and RankDisplay:FindFirstChildWhichIsA("UIStroke") then

				RankDisplay:FindFirstChildWhichIsA("UIGradient"):Destroy()

				RankDisplay:FindFirstChildWhichIsA("UIStroke"):Destroy()

			end
			
			if not RankDisplay:FindFirstChildWhichIsA("UIGradient") and not RankDisplay:FindFirstChildWhichIsA("UIStroke") then
				
				local ClonedGradient = HighRankUIDesigns:FindFirstChild(ChosenRank).Gradient:Clone()
				ClonedGradient.Parent = RankDisplay
				
				local ClonedTextOutline = HighRankUIDesigns:FindFirstChild(ChosenRank).TextOutline:Clone()
				ClonedTextOutline.Parent = RankDisplay

			end
			
		else
			
			RankDisplay.TextColor3 = RankColors[ChosenRank]
			
			if RankDisplay:FindFirstChildWhichIsA("UIGradient") and RankDisplay:FindFirstChildWhichIsA("UIStroke") then
				
				RankDisplay:FindFirstChildWhichIsA("UIGradient"):Destroy()
				
				RankDisplay:FindFirstChildWhichIsA("UIStroke"):Destroy()
				
			end
				
		end
		
		task.wait(0.25)
		
	until Rolls == 5
	
	task.wait(1)
	
	local PlrRankPriority = RankPriorities[plr.leaderstats.Rank.Value]
	
	local ChosenRankPriority = RankPriorities[ChosenRank]
	
	if PlrRankPriority == ChosenRankPriority then
		
		local PlrRankVal = plr.leaderstats.Value.Value
		
		if PlrRankVal < ChosenRankVal then
			
			RankRevealFrameHolder.RolledRankOptionsHolder.Visible = true
			
			RankRevealFrameHolder.RolledRankOptionsHolder.LeftOption.RewardType.Value = "RankVal"
			
		else
			
			TS:Create(RollButton,TweenStyle,{Size = RollButtonNormSize}):Play()
			
			RollButton.TextColor3 = Color3.fromRGB(239, 239, 239)
			
			TS:Create(RankRevealFrameHolder,TweenStyle,{Position = RankRevealFrameHolderPositions.In}):Play()

			task.wait(0.3)
			
			db = false
			
		end
		
	elseif PlrRankPriority < ChosenRankPriority then
		
		RankRevealFrameHolder.RolledRankOptionsHolder.Visible = true
		
		RankRevealFrameHolder.RolledRankOptionsHolder.LeftOption.RewardType.Value = "Overall"
		
	else
		
		TS:Create(RollButton,TweenStyle,{Size = RollButtonNormSize}):Play()
		
		RollButton.TextColor3 = Color3.fromRGB(239, 239, 239)
		
		TS:Create(RankRevealFrameHolder,TweenStyle,{Position = RankRevealFrameHolderPositions.In}):Play()

		task.wait(0.3)
		
		db = false
		
	end
	
	local CustomTS = TweenInfo.new(2,Enum.EasingStyle.Sine,Enum.EasingDirection.InOut)

	local NegTween

	local PosTween

	repeat
		
		NegTween = TS:Create(RankDisplay,CustomTS,{Rotation = -2.5})

		NegTween:Play()

		task.wait(2)

		PosTween = TS:Create(RankDisplay,CustomTS,{Rotation = 2.5})

		PosTween:Play()

		task.wait(2)

	until ChosenRank ~= "Legendary" or ChosenRank ~= "Mythic" -- RankDisplay.Text ~= "Legendary" or RankDisplay.Text ~= "Mythic"

	print(ChosenRank)
	
	NegTween:Cancel()
	
	PosTween:Cancel()

	RankDisplay.Rotation = 0
	
end

RollButton.MouseButton1Click:Connect(function()
	
	if not db then
		
		Roll()
		
	end
	
end)

local UIS = game:GetService("UserInputService")

UIS.InputBegan:Connect(function(input,GPE)
	
	if not GPE then
		
		if input.UserInputType == Enum.UserInputType.MouseButton1 then
			
			if not db then

				Roll()

			end
			
		end
		
	end
	
end)

local LeftOption = RolledRankOptionsHolder.LeftOption

LeftOption.MouseButton1Click:Connect(function()
	
	if LeftOption.RewardType.Value == "RankVal" then
		
		RewardPlrRoll:FireServer(LeftOption.RewardType.Value,ChosenRankVal)
		
	elseif LeftOption.RewardType.Value == "Overall" then

		RewardPlrRoll:FireServer(LeftOption.RewardType.Value,ChosenRank,ChosenRankVal)
		
	end
	
	RankRevealFrameHolder.RolledRankOptionsHolder.Visible = false
	
	TS:Create(RollButton,TweenStyle,{Size = RollButtonNormSize}):Play()
	
	RollButton.TextColor3 = Color3.fromRGB(239, 239, 239)
	
	TS:Create(RankRevealFrameHolder,TweenStyle,{Position = RankRevealFrameHolderPositions.In}):Play()
	
	task.wait(0.3)
	
	db = false
	
end)

Issue:

repeat
		
		NegTween = TS:Create(RankDisplay,CustomTS,{Rotation = -2.5})

		NegTween:Play()

		task.wait(2)

		PosTween = TS:Create(RankDisplay,CustomTS,{Rotation = 2.5})

		PosTween:Play()

		task.wait(2)

	until ChosenRank ~= "Legendary" or ChosenRank ~= "Mythic"

robloxapp-20240506-2245424.wmv (411.3 KB)

1 Like

Here you are using or, when it should be and… If the ChosenRank is “Mythic” then its not “Legendary”, and if its “Legendary” it’s not “Mythic” so always one or the other conditions are met. With and , if it’s not either then it will end.

local NegTween = TS:Create(RankDisplay,CustomTS,{Rotation = -2.5})
local PosTween = TS:Create(RankDisplay,CustomTS,{Rotation = 2.5})
repeat
		NegTween:Play()
		NegTween.Completed:Wait()
		PosTween:Play()
        PosTween.Completed:Wait()
until ChosenRank ~= "Legendary" or ChosenRank ~= "Mythic"

There is no need to create a new tween each time it runs, as you can reuse them, you can also wait for the tweens to complete before playing the next one.
Hope this helps.

1 Like

The problem is someone could instantly roll again after the cooldown resetting it

How do we know the ChosenRank ain’t Legendary if it ain’t mythic? The rotation tween should play for legendary and mythic

First.
You could let the cool down run then reset it.

Second.
You are using the repeat loop to continue until the condition is met. Using an or with negative comparators ~= means it will always meet one of the conditions cos if it is one it ain’t the other so will return true and end the repeat.
If it’s and it needs to not be either before the repeat loops stop.

2 Likes

Ok I finally understand. But now how do I check every milisecond or so of the db is false then I would stop the rotation comepletely bc once the db is false then the roll ui is off the screen

Could I do something like:


local NegTween = TS:Create(RankDisplay,CustomTS,{Rotation = -2.5})
local PosTween = TS:Create(RankDisplay,CustomTS,{Rotation = 2.5})
repeat
		NegTween:Play()
		NegTween.Completed:Wait()
		PosTween:Play()
        while PosTween.PlaybackState == Enum.PlaybackState.Playing do
if db == false then
break
end
until ChosenRank ~= "Legendary" or ChosenRank ~= "Mythic"

1 Like

That won’t work cos the while loop will stop the repeat loop from repeating until the while loop finishes.

You could connect an event to the gui frame’s visibility property to cancel the tweens and change a variable which would break the repeat loop.

guiElement:GetPropertyChangedSignal("Visible"):Once(function()
    if not guiElement.Visible then
      negTween:Cancel()
      posTween:Cancel()
      dB = false
    end
end)

Something like that, it would just need to have the tweens and db in scope, and be defined before the repeat loop. (I’m assuming other code moves the frames off screen and sets the visibility to false)