Is there any way to make my code look cleaner?

hey guys, I’ve been using task.spawn() to make a new thread in the background of my currently running code, is there any way to make it look… a bit cleaner?

it looks a lot like unnessary clutter on my code

I remember somebody talking about actors but I’m not too sure if that’s the solution (I also don’t know how to use it lol)

if yall have any solutions for this, please help me out! thank youu :))

Yes, you should adhere to some programming principles. Take a look at the style guide
http://lua-users.org/wiki/LuaStyleGuide

Module scripts can help you clean up your code too, if you properly move reusable code to modules.

Also, I’d avoid unnamed functions. It’s much easier to keep track of everything if you name your functions.

2 Likes

I would say, with my own preferences,

-Avoid using abbreviations like hrp, HumanoidRootPart is way more understandable.
-Localing with _ like body_part is way more uglier then bodyPart or BodyPart for me.
-Use comments which will create more empty space and make you understand it faster in the future.

1 Like

There’s no need to use actors for this. task.spawn is perfectly fine. If you don’t like the way the block looks in the middle of everything else, put the anonymous function into a named one and call task.spawn with the named function:

local function Animate()
    -- code goes here
end

local function EmitParticles()
    -- code goes here
end

-- Later on...
task.spawn(Animate)

task.spawn(EmitParticles)

If you need to pass in arguments to the functions, pass them after the function name:

local function foo(a, b, c)
    return a + b + c
end

task.spawn(foo, 10, 11, 12)
-- is equivalent to
task.spawn(function()
    foo(10, 11, 12)
end)

oooh okay, I’ll try naming my functions!

@sinanxd2 it’s usually my coding style, plus I feel like it’s really cluttered, and when I wanna avoid confusion I just do char_hrp… etc.

oh I didn’t know that worked, thank you so much! :))

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