How to Improve this script?

I am starting doing thi type of post to see what I could’ve done with my script to make it better, organized and optimized.

--[[VARIABLES]]--

local Frame = script.Parent
local information = script.Parent.Information
local star = Frame.Information.SaveStar
local image1,image2,image3,image4 = Frame.Bone1,Frame.Bone2,Frame.Bone3,Frame.Bone4

local tween = game:GetService("TweenService")
local corount = coroutine.wrap
local Childrens = script.Parent:GetChildren()

local offset = 0
local sound = Frame.Selection

--[[TABLES]]--

local images = {image1,image2,image3,image4}

--[[FUNCTIONS]]--


local function PlayTween(tween : Tween)
	tween:Play()
end


local function RotateImage()
	while task.wait(math.random(1,10)) do
		local random = images[math.random(1,#images)]
		local rotate = tween:Create(random,TweenInfo.new(1.5,Enum.EasingStyle.Circular,Enum.EasingDirection.Out),{Rotation = random.Rotation + 360})

		PlayTween(rotate)
	end
end

local function RotateStar()
	local rotateStar = tween:Create(star,TweenInfo.new(2.25,Enum.EasingStyle.Linear,Enum.EasingDirection.In,-1),{Rotation = star.Rotation + 360})
	PlayTween(rotateStar)
end

local function Tween(object : Instance, Position : UDim2)
	local info = TweenInfo.new(1, Enum.EasingStyle.Quart, Enum.EasingDirection.InOut, -1, true, 0)
	local property = {

		Position = Position

	}
	local pos = tween:Create(object,info,property)

	PlayTween(pos)
end

local function MouseEnter(object : Instance,color : Color3)
	object.BorderColor3 = color
end

local function MouseLeave(object : Instance,original : Color3)
	object.BorderColor3 = original
end

for _,v in pairs(information:GetChildren()) do
	if v:IsA("TextButton") then
		local fromRGB = Color3.fromRGB
		local color = fromRGB(255,255,0)
		local original = v.BorderColor3
		v.MouseEnter:Connect(function()
			MouseEnter(v,color)
			sound:Play()
		end)
		v.MouseLeave:Connect(function()
			MouseLeave(v,original)
		end)
	end
end

--[[Threads]]--

task.spawn(RotateImage)
task.spawn(RotateStar)
local core1 = corount(Tween)(image1,UDim2.new(0.015,offset,0.1,offset))
local core2 = corount(Tween)(image2,UDim2.new(0.985,offset,0.9,offset))
local core3 = corount(Tween)(image3,UDim2.new(0.05,offset,0.045,offset))
local core4 = corount(Tween)(image4,UDim2.new(0.95,offset,0.96,offset))
2 Likes

Answer these questions please (in quotes):
General Questions:

  • Does it work?
  • What does it do?

Code:

  • You defined corout - however, you used task.spawn() later. Why?
  • You defined core1, core2, core3 and core4, but never used them. Why?

Now I may be wrong on this one:
You defined corout as coroutine.wrap and fromRGB as Color3.fromRGB. As far as I know, you would not be able to use these. Instead, just do coroutine.wrap(thisfunction()().

Notes:
If I am correct, the function Tween does not need a coroutine as it does not yield the script.

I already fixed that and didn’t used coroutine anymore since the tween still plays. So that’s ok about it. Also it works, just need to know if that needs some improvement

--[[VARIABLES]]--

local Frame = script.Parent
local information = script.Parent.Information
local star = Frame.Information.SaveStar
local image1,image2,image3,image4 = Frame.Bone1,Frame.Bone2,Frame.Bone3,Frame.Bone4

local tween = game:GetService("TweenService")
local thread = task.spawn
local Childrens = information:GetChildren()

local offset = 0
local sound = Frame.Selection

--[[TABLES]]--

local images = {image1,image2,image3,image4}

--[[FUNCTIONS]]--


local function PlayTween(tween : Tween)
	tween:Play()
end


local function RotateImage()
	local tweenRotate = TweenInfo.new(1.5, Enum.EasingStyle.Circular, Enum.EasingDirection.Out)
	while task.wait(math.random(3,10)) do
		local random = images[math.random(1,#images)]
		local rotate = tween:Create(random,tweenRotate,{Rotation = random.Rotation + 360})

		PlayTween(rotate)
	end
end

local function RotateStar()
	local rotateStar = tween:Create(star,TweenInfo.new(2.25,Enum.EasingStyle.Linear,Enum.EasingDirection.In,-1),{Rotation = star.Rotation + 360})
	PlayTween(rotateStar)
end		

local function Tween(object : Instance, Position : UDim2)
	local info = TweenInfo.new(1, Enum.EasingStyle.Quart, Enum.EasingDirection.InOut, -1, true, 0)
	local property = {

		Position = Position

	}
	
	local pos = tween:Create(object,info,property)

	PlayTween(pos)
end

local lastPlayTime = 0 

local function PlaySound(sound)
	local currentTime = tick()
	if currentTime - lastPlayTime > .2 then 
		sound:Play()
		lastPlayTime = currentTime 
	end
end

local function MouseEnter(object : Instance,color : Color3)
	object.BorderColor3 = color
	PlaySound(sound)	
end	

local function MouseLeave(object : Instance,original : Color3)
	object.BorderColor3 = original
end

for _,v in pairs(Childrens) do
	if v:IsA("TextButton") then
		local fromRGB = Color3.fromRGB
		local color = fromRGB(255,255,0)
		local original = v.BorderColor3
		v.MouseEnter:Connect(function()
			MouseEnter(v,color)
		end)
		v.MouseLeave:Connect(function()
			MouseLeave(v,original)
		end)
	end
end

--[[Threads]]--

thread(RotateImage)
thread(RotateStar)

--[[FunctionCalls]]--

local core1 = (Tween)(image1,UDim2.new(0.015,offset,0.1,offset))
local core2 = (Tween)(image2,UDim2.new(0.985,offset,0.9,offset))
local core3 = (Tween)(image3,UDim2.new(0.05,offset,0.045,offset))
local core4 = (Tween)(image4,UDim2.new(0.95,offset,0.96,offset))

Like what the guy before said why did you define 4 new variables at the end when you are not even using them.

I already fixed that… As I said before.

Also, the coroutines functions is being used since I am calling it right when I set the variable!

Show us your new script?

(remove30charrule)

1 Like
--[[VARIABLES]]--

local Frame = script.Parent
local frame2 = Frame.Parent
local information = script.Parent.Information
local star = information.SaveStar
local image1,image2,image3,image4 = frame2.Bone1,frame2.Bone2,frame2.Bone3,frame2.Bone4
local textlabel = script.Parent.Stats.SansName --change

local tween = game:GetService("TweenService")
local thread = task.spawn
local Childrens = information:GetChildren()

local offset = 0
local sound = game:GetService("ReplicatedStorage").Selection

--[[TABLES]]--

local images = {image1,image2,image3,image4}

--[[FUNCTIONS]]--

local function PlayTween(tween : Tween)
	tween:Play()
end


local function RotateImage()
	local tweenRotate = TweenInfo.new(.6, Enum.EasingStyle.Quad, Enum.EasingDirection.Out)
	while task.wait(math.random(3,10)) do
		local random = images[math.random(1,#images)]
		local rotate = tween:Create(random,tweenRotate,{Rotation = random.Rotation + 360})

		PlayTween(rotate)
	end
end

local function RotateStar()
	local rotateStar = tween:Create(star,TweenInfo.new(2.25,Enum.EasingStyle.Linear,Enum.EasingDirection.In,-1),{Rotation = star.Rotation + 360})
	PlayTween(rotateStar)
end		

local function Tween(object : Instance, Position : UDim2)
	local info = TweenInfo.new(.8, Enum.EasingStyle.Exponential, Enum.EasingDirection.InOut, -1, true, 0)
	local property = {

		Position = Position

	}

	local pos = tween:Create(object,info,property)

	PlayTween(pos)
end


local function PlaySound(sound : Sound) -- Here is the sound function
	local clone = sound:Clone()
	clone.Parent = Frame
	clone:Play()
	game.Debris:AddItem(clone,clone.TimeLength)
	if clone.IsPlaying == true then
		print("Som está tocando")
	else
		print("Som não está tocando")
	end
end

for _,v in pairs(Childrens) do
	if v:IsA("TextButton") then
		v.MouseEnter:Connect(function()
			PlaySound(sound)
		end)
	end
end

--[[Threads]]--

thread(RotateImage)
thread(RotateStar)

--[[FunctionCalls]]--

local core1 = (Tween)(image1,UDim2.new(0.015,offset,0.1,offset))
local core2 = (Tween)(image2,UDim2.new(0.985,offset,0.9,offset))
local core3 = (Tween)(image3,UDim2.new(0.05,offset,0.045,offset))
local core4 = (Tween)(image4,UDim2.new(0.95,offset,0.96,offset))

This script handle tweens, sounds and other stuff on the Menu.

I am witnessing with a bug that the sound on “sound function” sometimes Doesn’t play.

you’re defining core1, core2, core3 and core4 as a function. though the function does not return anything. it’d be better without defining it.

just call the tween like this:
Tween(parameters)

--[[VARIABLES]]--

local Frame = script.Parent
local frame2 = Frame.Parent
local information = script.Parent.Information
local star = information.SaveStar
local image1,image2,image3,image4 = frame2.Bone1,frame2.Bone2,frame2.Bone3,frame2.Bone4
local textlabel = script.Parent.Stats.SansName --change

local tween = game:GetService("TweenService")
local thread = task.spawn
local Childrens = information:GetChildren()

local offset = 0
local sound = game:GetService("ReplicatedStorage").Selection

--[[TABLES]]--

local images = {image1,image2,image3,image4}

--[[FUNCTIONS]]--

local function PlayTween(tween : Tween)
	tween:Play()
end


local function RotateImage()
	local tweenRotate = TweenInfo.new(.6, Enum.EasingStyle.Quad, Enum.EasingDirection.Out)
	while task.wait(math.random(3,10)) do
		local random = images[math.random(1,#images)]
		local rotate = tween:Create(random,tweenRotate,{Rotation = random.Rotation + 360})

		PlayTween(rotate)
	end
end

local function RotateStar()
	local rotateStar = tween:Create(star,TweenInfo.new(2.25,Enum.EasingStyle.Linear,Enum.EasingDirection.In,-1),{Rotation = star.Rotation + 360})
	PlayTween(rotateStar)
end		

local function Tween(object : Instance, Position : UDim2)
	local info = TweenInfo.new(.8, Enum.EasingStyle.Exponential, Enum.EasingDirection.InOut, -1, true, 0)
	local property = {

		Position = Position

	}

	local pos = tween:Create(object,info,property)

	PlayTween(pos)
end


local function PlaySound(sound : Sound) -- Here is the sound function
	local clone = sound:Clone()
	clone.Parent = Frame
	clone:Play()
	game.Debris:AddItem(clone,clone.TimeLength)
	if clone.IsPlaying == true then
		print("Som está tocando")
	else
		print("Som não está tocando")
	end
end

for _,v in pairs(Childrens) do
	if v:IsA("TextButton") then
		v.MouseEnter:Connect(function()
			PlaySound(sound)
		end)
	end
end

--[[Threads]]--

thread(RotateImage)
thread(RotateStar)

--[[FunctionCalls]]--
Tween(image1,UDim2.new(0.015,offset,0.1,offset))
Tween(image2,UDim2.new(0.985,offset,0.9,offset))
Tween(image3,UDim2.new(0.05,offset,0.045,offset))
Tween(image4,UDim2.new(0.95,offset,0.96,offset))

RotateStar() does not need a separate thread. This leaves us with one thread created, making the variable useless

--[[VARIABLES]]--

local Frame = script.Parent
local frame2 = Frame.Parent
local information = script.Parent.Information
local star = information.SaveStar
local image1,image2,image3,image4 = frame2.Bone1,frame2.Bone2,frame2.Bone3,frame2.Bone4
local textlabel = script.Parent.Stats.SansName --change

local tween = game:GetService("TweenService")
local Childrens = information:GetChildren()

local offset = 0
local sound = game:GetService("ReplicatedStorage").Selection

--[[TABLES]]--

local images = {image1,image2,image3,image4}

--[[FUNCTIONS]]--

local function PlayTween(tween : Tween)
	tween:Play()
end


local function RotateImage()
	local tweenRotate = TweenInfo.new(.6, Enum.EasingStyle.Quad, Enum.EasingDirection.Out)
	while task.wait(math.random(3,10)) do
		local random = images[math.random(1,#images)]
		local rotate = tween:Create(random,tweenRotate,{Rotation = random.Rotation + 360})

		PlayTween(rotate)
	end
end

local function RotateStar()
	local rotateStar = tween:Create(star,TweenInfo.new(2.25,Enum.EasingStyle.Linear,Enum.EasingDirection.In,-1),{Rotation = star.Rotation + 360})
	PlayTween(rotateStar)
end		

local function Tween(object : Instance, Position : UDim2)
	local info = TweenInfo.new(.8, Enum.EasingStyle.Exponential, Enum.EasingDirection.InOut, -1, true, 0)
	local property = {

		Position = Position

	}

	local pos = tween:Create(object,info,property)

	PlayTween(pos)
end


local function PlaySound(sound : Sound) -- Here is the sound function
	local clone = sound:Clone()
	clone.Parent = Frame
	clone:Play()
	game.Debris:AddItem(clone,clone.TimeLength)
	if clone.IsPlaying == true then
		print("Som está tocando")
	else
		print("Som não está tocando")
	end
end

for _, v in pairs(Childrens) do
	if v:IsA("TextButton") then
		v.MouseEnter:Connect(function()
			PlaySound(sound)
		end)
	end
end

--[[FunctionCalls]]--
task.spawn(RotateImage)
RotateStar()
Tween(image1,UDim2.new(0.015,offset,0.1,offset))
Tween(image2,UDim2.new(0.985,offset,0.9,offset))
Tween(image3,UDim2.new(0.05,offset,0.045,offset))
Tween(image4,UDim2.new(0.95,offset,0.96,offset))

Thank you! It helped me a little. But then why when I stored the functions on a variable it was being called? And why is my sound not playing sometimes it should?

it was fine calling the functions by defining it, but since you never used the defined variables, there was no use.
You normally define a variable to a function if the function returns something

1 Like

you don’t give it enough time to load the sound, meaning that when you play the sound, there is no sound to play

right after you clone the sound, do a task.wait()

1 Like

It works thank you! :>

This text willl be BLORrwd

Any more thought with the script ? What I can add, What I can delete?

1 Like

ThAt WaS WhAt I sAiD.

(nvm ._.)