#Any better way to do this?

1 messages · Page 1 of 1 (latest)

open ginkgo
#

All the code below works, though I was wondering if there is a more efficient way to do it all, as the codes looks really messy and i feel like as i add more onto it it'd become a pain to edit. I'm rather new to coding, was just wondering if there is a more efficient, clean way to code this.

twilit meadow
#

FadeVolume() seems overcomplicated I would tween a proxy and mirror the value to the Sound.Volume

Your naming for things don't seem consistent e.g. wehn to use upper characters and lower characters

I would wrap a framework like this up in a module and use that in a normal script instead of having the whole logic in a normal script

open ginkgo
#

alr, thanks

open ginkgo
gloomy viper
# open ginkgo

Connection = game:GetService("RunService").Heartbeat:Connect(function()
Avoid getting services in the middle of your code, always explicitly define them at the top

Use os.clock() instead of tick() (explanation here: https://create.roblox.com/docs/reference/engine/globals/RobloxGlobals#tick)

Sound.Volume = StartVolume + (targetVolume - StartVolume) * Progress
The math library now has a lerp function: Sound.Volume = math.lerp(StartVolume, targetVolume, Progress)

Your FadeVolume function is fine, tweening a proxy instance is jank
(though I agree with zyos, that function should be a module script)

for i, player in players do
Unused variables can be replaced with _
for _, player in players do

I would not support an optional second argument in your Countdown function since the functionality becomes nearly the same as task.wait(), meaning that should be used instead

open ginkgo
muted fog
velvet kettleBOT
#

studio** You are now Level 1! **studio

muted fog
open ginkgo
# muted fog Ur welcome

Ah I get it now, so the benefit is that that function will now be usable in every script by just using require(blah blah blah) , making it so I don't have to rewrite it.

muted fog
#

Correct

open ginkgo
# gloomy viper `Connection = game:GetService("RunService").Heartbeat:Connect(function()` Avoid ...

Okay so:
I've changed tick() to os.clock()
Changed Sound.Volume = StartVolume + (targetVolume - StartVolume) * Progress to Sound.Volume = math.lerp(StartVolume, targetVolume, Progress)
Put game:GetService("RunService") to happen at the start of the script
Put the FadeVolume() function into a module script.

I have added some extra code just to keep the workspace tidy as I add onto it in the future, just wondering if I've done the following correctly, as once again I'm quite new to coding and this is my first time using a module script.

gloomy viper
gloomy viper
# open ginkgo Okay so: I've changed `tick()` to `os.clock()` Changed `Sound.Volume = StartVolu...

Modules scripts can return any datatype so for that module script I'd recommend doing this instead

return function(Sound, TargetVolume, Duration)
    local StartVolume = Sound.Volume
    local StartTime = os.clock()

    local Connection
    Connection = game:GetService("RunService").Heartbeat:Connect(function()
        local Elapsed = os.clock() - StartTime
        local Progress = math.min(Elapsed / Duration, 1)

        Sound.Volume = math.lerp(StartVolume, TargetVolume, Progress)

        if Progress >= 1 then
            Connection:Disconnect()
        end
    end)
end

And to use that you would then do

local FadeVolume = require(script.Parent.FadeVolumeModule)
FadeVolume(IntermissionSound, 0.05, 5)

I'd also place FadeVolumeModule in a Util folder in ReplicatedStorage and rename the module to FadeVolume to match the function name

#

There are also plugins that can give you auto imports, meaning if you type out all or part of FadeVolume in any file and press tab it will automatically find the module and add a require for it to the top of your file

open ginkgo
gloomy viper
# open ginkgo Any reccomended one?>

The one I use (luau lsp) is an extension for visual studio code and not roblox studio 😭
I know there are ones that exist for studio because I've seen them, I just don't know their names sorry

open ginkgo
open ginkgo
gloomy viper
#

(util folder is just a folder named Util, short for Utility)

open ginkgo
#

alr

gloomy viper
# gloomy viper Completely subjective organization

*putting it in replicated storage does serve a purpose though. Stuff in replicated storage is replicated to each client when they join, meaning code and assets that are inside of it can be accessed from local scripts

open ginkgo
velvet kettleBOT
#

studio** You are now Level 2! **studio

gloomy viper
open ginkgo
#

cool, thanks for helping out

open ginkgo
gloomy viper
#

Generally a simple solution would be a remote event that the client listens to and updates the text to any message it revives

#

Then from the server you can fire the event from anywhere to update the text

open ginkgo
#

oh alrighty

open ginkgo
gloomy viper
#

Have you used normal events? (like with :Connect)

#

Set up by adding a RemoteEvent called UpdateText to ReplicatedStorage/Events

A local script

local event = ReplicatedStorage.Events.UpdateText

event.OnClientEvent:Connect(function(msg)
  print(`Got "{msg}" from server`)
end)

A server script

local event = ReplicatedStorage.Events.UpdateText

event:FireAllClients("Hello, World!")

You could extend that by calling event:FireAllClients("Hello, World!") each time you loop in Cooldown and to set messages like event:FireAllClients("Waiting for players...")

On the client instead of printing you can place the script inside the UI and easily reference the text label to update its text
-# In the future if/when you start to notice that doing stuff that way gets difficult to maintain in large projects, check out Fusion & UILabs. They are much more advanced ways of making UI and share some similarities with modern web development frameworks. They have a lot of advantages with UI reusaiblity and modularity, but I'm not sure if I would recommend learning all of that now though because its a lot to learn and you realistically don't need it when just starting out. There are also networking libraries like Blink that handle argument validation and can reduce network load. Again I wouldn't recommend learning those now just kinda a thing to keep in mind for later (I'm glad I didn't immediately start doing that stuff but I wish I found out about it sooner so thats why I'm putting this huge wall of text here). Also theres Rojo which lets you use version control like git and other external tooling

open ginkgo