Why does this cause fps drops?

local abbreviations = {"", "K", "M", "B", "Qa", "Qi", "Sx", "Sp", "Oc", "No", "Dc", "Un", "Duo", "Tre", "Qua", "Qui", "Sed", "Sep", "Oct", "Nov", "V"}

local function Format(value, idp)
	local ex = math.floor(math.log(math.max(1, math.abs(value)),1000))
	local abbrevs = abbreviations [1 + ex] or ("e+"..ex)
	local normal = math.floor(value * ((10 ^ idp) / (1000 ^ ex))) / (10 ^ idp)

	return  ("%."..idp.."f%s"):format(normal, abbrevs)

end



local currentAmount = 0
local currentCoinAmount = 0


local plr = game.Players.LocalPlayer
local leaderstats = plr:FindFirstChild("leaderstats")
local ts  =game:GetService("TweenService")

task.wait(3)

plr.leaderstats.experience.Changed:Connect(function()
	task.delay(0.2, function()
		if currentAmount ~= leaderstats.experience.Value  then
			local rando = math.random(1,1500)
		local randox = rando/ 1400
		local NewLabel = game.ReplicatedStorage.ExpPopup:Clone()
        
		NewLabel.Parent = script.Parent
		ts:Create(NewLabel,TweenInfo.new(0.2,Enum.EasingStyle.Cubic), {Size = UDim2.new(0.070, 0,0.120, 0)}):Play()
		NewLabel.Position = UDim2.new(randox,randox,randox,randox)
		ts:Create(NewLabel,TweenInfo.new(0.3,Enum.EasingStyle.Cubic),{Rotation = 95}):Play()
		NewLabel.ExpTextPopUp.Text = Format(plr.leaderstats.experience.Value - currentAmount,1)
		currentAmount = plr.leaderstats.experience.Value
		
		ts:Create(NewLabel,TweenInfo.new(0.3,Enum.EasingStyle.Cubic),{Rotation = 0}):Play()
		task.wait(0.3)
		ts:Create(NewLabel,TweenInfo.new(0.3,Enum.EasingStyle.Cubic), {Size = UDim2.new(0,0,0,0)}):Play()
		ts:Create(NewLabel,TweenInfo.new(0.3,Enum.EasingStyle.Cubic),{ImageTransparency =1}):Play()
		ts:Create(NewLabel.ExpTextPopUp,TweenInfo.new(0.6,Enum.EasingStyle.Cubic),{TextTransparency =1}):Play()
			ts:Create(NewLabel.ExpTextPopUp.UIStroke,TweenInfo.new(0.6,Enum.EasingStyle.Cubic),{Transparency =1}):Play()
         game.Debris:AddItem(NewLabel, .35)

	end
	end)
	

end)

this is called when the leaderstats is changed and creates a pop up on the screen after doing this for a while it stars to drop fps. does anybody know why? it is whenever the pop up appears and tweens.

4 Likes

A new tween is being created each time the xp changes instead you should declare them before the function and Play them in the function.

2 Likes

function TweenFirst(NewLabel)
	ts:Create(NewLabel,TweenInfo.new(0.2,Enum.EasingStyle.Cubic), {Size = UDim2.new(0.070, 0,0.120, 0)}):Play()
	ts:Create(NewLabel,TweenInfo.new(0.3,Enum.EasingStyle.Cubic),{Rotation = 95}):Play()
end

function TweenSecond(NewLabel)
	ts:Create(NewLabel,TweenInfo.new(0.3,Enum.EasingStyle.Cubic), {Size = UDim2.new(0,0,0,0)}):Play()
	ts:Create(NewLabel,TweenInfo.new(0.3,Enum.EasingStyle.Cubic),{ImageTransparency =1}):Play()
	ts:Create(NewLabel.ExpTextPopUp,TweenInfo.new(0.6,Enum.EasingStyle.Cubic),{TextTransparency =1}):Play()
	ts:Create(NewLabel.ExpTextPopUp.UIStroke,TweenInfo.new(0.6,Enum.EasingStyle.Cubic),{Transparency =1}):Play()
end

plr.leaderstats.experience.Changed:Connect(function()
	task.delay(0.2, function()
		if currentAmount ~= leaderstats.experience.Value  then
			local rando = math.random(1,1500)
		local randox = rando/ 1400
		local NewLabel = game.ReplicatedStorage.ExpPopup:Clone()
        
		NewLabel.Parent = script.Parent
		TweenFirst(NewLabel)
		NewLabel.Position = UDim2.new(randox,randox,randox,randox)
		
		NewLabel.ExpTextPopUp.Text = Format(plr.leaderstats.experience.Value - currentAmount,1)
		currentAmount = plr.leaderstats.experience.Value
		
		
		task.wait(0.3)
		TweenSecond(NewLabel)
         game.Debris:AddItem(NewLabel, .35)

	end
	end)
	

end)

like this or should i create a seperate variable for each tween?

kind of better but what I meant was that A tween was being created each time the code was ran.

function Tweenthings()
    ts:create(...):Play()
    --A new tween is being created in this function
end

it should be

local Tween1=ts:create(...)
function Tweenthings()
    Tween1:Play()
--it takes less time because it is using a tween it created before
end
2 Likes

Ohhh thank you this works!

,

1 Like

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