Is there a way to optimize my code?

I am making a script for GUI that clones onto the players screen and soon after gets removed. My code feels as if it isn’t optimized and I was wondering if there is a better way to make it?
Code:

local plr = game.Players.LocalPlayer
local RS = game:GetService("ReplicatedStorage")
local TS = game:GetService("TweenService")

RS.Remotes.StatAddGui.OnClientEvent:Connect(function(guiType, duraAdded, strAdded)
	if guiType == "DuraAddGui" then
		local newDuraGui = RS.Gui.ReplicatedGui.DuraAddFrame:Clone()
		newDuraGui.Parent = plr.PlayerGui.ClonedGui
		local Xpos = math.random(280, 700)/1000
		local Ypos = math.random(100, 750)/1000
		local info1 = TweenInfo.new(.6)
		local info2 = TweenInfo.new(.8)
		
		newDuraGui.DuraAddedLabel.Text = "+"..duraAdded.." Durability"
		newDuraGui.Position = UDim2.new(Xpos, 0, Ypos, 0)
		
		TS:Create(newDuraGui, info1, {Position = newDuraGui.Position + UDim2.new(0, 0, .03, 0)}):Play()
		wait(.5)
		TS:Create(newDuraGui, info2, {Position = newDuraGui.Position + UDim2.new(.05, 0, .05, 0)}):Play()
		TS:Create(newDuraGui.DuraAddedLabel, info2, {TextTransparency = 1}):Play()
		TS:Create(newDuraGui.DuraIcon, info2, {ImageTransparency = 1}):Play()
		wait(.8)
		newDuraGui:Destroy()
	elseif guiType == "StrAddGui" then
		local newStrGui = RS.Gui.ReplicatedGui.StrengthAddFrame:Clone()
		newStrGui.Parent = plr.PlayerGui.ClonedGui
		local Xpos = math.random(280, 700)/1000
		local Ypos = math.random(100, 750)/1000
		local info1 = TweenInfo.new(.6)
		local info2 = TweenInfo.new(.8)

		newStrGui.StrAddedLabel.Text = "+"..strAdded.." Strength"
		newStrGui.Position = UDim2.new(Xpos, 0, Ypos, 0)

		TS:Create(newStrGui, info1, {Position = newStrGui.Position + UDim2.new(0, 0, .03, 0)}):Play()
		wait(.5)
		TS:Create(newStrGui, info2, {Position = newStrGui.Position + UDim2.new(.05, 0, .05, 0)}):Play()
		TS:Create(newStrGui.StrAddedLabel, info2, {TextTransparency = 1}):Play()
		TS:Create(newStrGui.StrIcon, info2, {ImageTransparency = 1}):Play()
		wait(.8)
		newStrGui:Destroy()
	end
end)

As you can see, I have two sections that are basically the same except they are for different stats.

1 Like

You can make a function that applies the guiType, including the if and elseif as the blocks, and the varied information to apply within the arguments, such as " Strength" and " Durability", and more!