Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Any better way of writing this long code? The code does work but I need help for a better way?

Sorry for the bad code. I want to learn better, that's why I am posting this. THIS CODE WORKS! But, is there a proper way of doing it?

--//Variables
local Player = game.Players.LocalPlayer

local Play = script.Parent.Intro.Container.Play
local Loadout = script.Parent.Intro.Container.Loadout
local Settings = script.Parent.Intro.Container.Settings

repeat wait() until Player.Character

--// Fade Functions
function FadePlay()
	for i = 1,.5,-.05 do
		Play.BackgroundTransparency = i
		wait(.001)
	end
end

function FadeLoadout()
	for i = 1,.5,-.05 do
		Loadout.BackgroundTransparency = i
		wait(.001)
	end
end

function FadeSettings()
	for i = 1,.5,-.05 do
		Settings.BackgroundTransparency = i
		wait(.001)
	end
end

function UnFadePlay()
	for i = .5,1,.05 do
		Play.BackgroundTransparency = i
		wait(.001)
	end
end

function UnFadeLoadout()
	for i = .5,1,.05 do
		Loadout.BackgroundTransparency = i
		wait(.001)
	end
end

function UnFadeSettings()
	for i = .5,1,.05 do
		Settings.BackgroundTransparency = i
		wait(.001)
	end
end

--// Mouse Enter
Play.MouseEnter:connect(function()
	FadePlay()
end)

Play.MouseLeave:connect(function()
	UnFadePlay()
end)

Loadout.MouseEnter:connect(function()
	FadeLoadout()
end)

Loadout.MouseLeave:connect(function()
	UnFadeLoadout()
end)

Settings.MouseEnter:connect(function()
	FadeSettings()
end)

Settings.MouseLeave:connect(function()
	UnFadeSettings()
end)

1 comment

0

honestly thats the best way to do it. sometimes code just has to be long. I have a 46 line script just to calcuate the health of soemthing, destroy it if the health gets to low, and to display it to a gui. Sometimes its just unavoidable.

1 answer

✓ Accepted answer

You could combine your functions.

Each function is pointing to another function, which makes the code longer. Just combine these functions. Example,

--//Variables
local Player = game.Players.LocalPlayer
local Play = script.Parent.Intro.Container.Play
local Loadout = script.Parent.Intro.Container.Loadout
local Settings = script.Parent.Intro.Container.Settings

repeat wait() until Player.Character

--// Mouse Enter
Play.MouseEnter:connect(function()
    for i = 1,.5,-.05 do
        Play.BackgroundTransparency = i
        wait()
    end
end)

Play.MouseLeave:connect(function()
    for i = .5,1,.05 do
        Play.BackgroundTransparency = i
        wait()
    end
end)

Loadout.MouseEnter:connect(function()
    for i = 1,.5,-.05 do
        Loadout.BackgroundTransparency = i
        wait()
    end
end)

Loadout.MouseLeave:connect(function()
    for i = .5,1,.05 do
        Loadout.BackgroundTransparency = i
        wait()
    end
end)

Settings.MouseEnter:connect(function()
    for i = 1,.5,-.05 do
        Settings.BackgroundTransparency = i
        wait()
    end
end)

Settings.MouseLeave:connect(function()
    for i = .5,1,.05 do
        Settings.BackgroundTransparency = i
        wait()
    end
end)
I would also suggest using more WaitForChild functions towards to top of the script.

That's about all I can think of without affecting how the script works and functions.

Good Luck!

This discussion is preserved as it was. New answers, comments, and votes are closed.