Would this be considered spagetti code?

hello, i’m always super scared to start scripting because i feel like whatever i make is too ‘messy’, and i really hate how demotivating the ‘professional oop’ approach is. in order to prevent my state machine/animate script from being leaked i’ll just show my variables. i need some genuine advice, i tried putting a lot of spaces between the blocks to show that they’re seperated, but i don’t know if that’ll be good enough later on.

local players = game:GetService("Players")
local runService = game:GetService("RunService")
local userInputService = game:GetService("UserInputService")
local replicatedStorage = game:GetService("ReplicatedStorage")

local plr = players.LocalPlayer
local character = plr.Character
local humanoid = character:FindFirstChildWhichIsA("Humanoid")
local animator = humanoid:FindFirstChildWhichIsA("Animator")
local rootPart = character.PrimaryPart

local testEvent = game:GetService("ReplicatedStorage"):WaitForChild("Events"):WaitForChild("NewStat")
 -- was for debugging forever ago just ignore this ^^^


local controller = character:FindFirstChildWhichIsA("ControllerManager")
local groundSensor = rootPart:WaitForChild("GroundSensor")
local config = character:WaitForChild("Status")
local walkSpeed = config:GetAttribute("WalkSpeed")
local sprintSpeed = config:GetAttribute("SprintSpeed")
local stamina = config:GetAttribute("Stamina")



local animations = replicatedStorage:WaitForChild("Assets"):WaitForChild("Animations"):WaitForChild("Character")

local state = "Walk"
local walkAnim = animator:LoadAnimation(animations:WaitForChild("Walk"))
local slideAnim = animator:LoadAnimation(animations:WaitForChild("Slide"))
local mantleAnim = animator:LoadAnimation(animations:WaitForChild("Mantle"))
local fallAnim = animator:LoadAnimation(animations:WaitForChild("Falling"))

task.wait(1) -- let animations load --

if not walkAnim or not slideAnim or not mantleAnim or not fallAnim then warn("animations failed to load") end

walkAnim.Looped = true
slideAnim.Looped = true
fallAnim.Looped = true
mantleAnim.Priority = Enum.AnimationPriority.Action2
fallAnim.Priority = Enum.AnimationPriority.Action2

local nan = 0.001

walkAnim:Play(0,nan,0)
slideAnim:Play(0,nan,0)

local isSlideCooldown = true
local slideCooldown = 1

local regenCooldown = 2
local canRegen = true
local regenCoolingDown = false

local mantle = false


local sprintCost = config:GetAttribute("SprintCost")
local jumpCost = config:GetAttribute("JumpCost")
local slideCost = config:GetAttribute("SlideCost")


local params = RaycastParams.new()
params.FilterType = Enum.RaycastFilterType.Exclude
params.FilterDescendantsInstances = {character}

lmk what you think, how i can improve it, etc.

thanks,

  • luke
1 Like

Organizing your services, objects, and variables is a good habit that makes your code more organized. Adding comments aswell is nice for when you look back.

-- Services
local Players = game:GetService("Players")
local RunService = game:GetService("RunService")
local UserInputService = game:GetService("UserInputService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

-- Player and Character
local Plr = Players.LocalPlayer
local Character = Plr.Character
local Humanoid = Character:FindFirstChildWhichIsA("Humanoid")
local Animator = Humanoid:FindFirstChildWhichIsA("Animator")
local RootPart = Character.PrimaryPart

-- Custom Event (for debugging, can ignore)
local TestEvent = ReplicatedStorage:WaitForChild("Events"):WaitForChild("NewStat")

-- Controller and Configuration
local Controller = Character:FindFirstChildWhichIsA("ControllerManager")
local GroundSensor = RootPart:WaitForChild("GroundSensor")
local Config = Character:WaitForChild("Status")

-- Attributes
local WalkSpeed = Config:GetAttribute("WalkSpeed")
local SprintSpeed = Config:GetAttribute("SprintSpeed")
local Stamina = Config:GetAttribute("Stamina")
local SprintCost = Config:GetAttribute("SprintCost")
local JumpCost = Config:GetAttribute("JumpCost")
local SlideCost = Config:GetAttribute("SlideCost")

-- Animations
local Animations = ReplicatedStorage:WaitForChild("Assets"):WaitForChild("Animations"):WaitForChild("Character")
local WalkAnim = Animator:LoadAnimation(Animations:WaitForChild("Walk"))
local SlideAnim = Animator:LoadAnimation(Animations:WaitForChild("Slide"))
local MantleAnim = Animator:LoadAnimation(Animations:WaitForChild("Mantle"))
local FallAnim = Animator:LoadAnimation(Animations:WaitForChild("Falling"))

-- Constants
local Nan = 0.001

-- Cooldowns
local SlideCooldown = 1
local RegenCooldown = 2

-- Flags
local IsSlideCooldown = true
local CanRegen = true
local RegenCoolingDown = false
local Mantle = false

-- Raycast Parameters
local Params = RaycastParams.new()
Params.FilterType = Enum.RaycastFilterType.Exclude
Params.FilterDescendantsInstances = {Character}

-- Let animations load
task.wait(1)

-- Check if animations failed to load
if not WalkAnim or not SlideAnim or not MantleAnim or not FallAnim then
    warn("Animations failed to load")
end

-- Set animation properties
WalkAnim.Looped = true
SlideAnim.Looped = true
FallAnim.Looped = true
MantleAnim.Priority = Enum.AnimationPriority.Action2
FallAnim.Priority = Enum.AnimationPriority.Action2

-- Play animations
local Nan = 0.001
WalkAnim:Play(0, Nan, 0)
SlideAnim:Play(0, Nan, 0)

2 Likes

thank you, my only problem with comments is fluency.

i used to add comments for everything, but after making so many scripts it started getting annoying.

this is what i used to do

-- services --

-- basic citings --

-- values --

-- functions --

-- connections --

but it got so tedious i just stopped doing it. i like your method of just freeforming it and explaining in one line simply what it’s for, makes sense for me.

2 Likes

okay, i just realized that studio assistant might be able to help me write annotations in my programs. it sounds a bit off, since it’s essentially the note form of a summary, however it might be useful. what do you think?

-- This code snippet sets up various services and variables needed for character movement and animations.

-- Get necessary services
local players = game:GetService("Players")
local runService = game:GetService("RunService")
local userInputService = game:GetService("UserInputService")
local replicatedStorage = game:GetService("ReplicatedStorage")

-- Get the local player and their character
local plr = players.LocalPlayer
local character = plr.Character
local humanoid = character:FindFirstChildWhichIsA("Humanoid")
local animator = humanoid:FindFirstChildWhichIsA("Animator")
local rootPart = character.PrimaryPart

-- Get a specific event from ReplicatedStorage
local testEvent = game:GetService("ReplicatedStorage"):WaitForChild("Events"):WaitForChild("NewStat")

-- Get additional character components and attributes
local controller = character:FindFirstChildWhichIsA("ControllerManager")
local groundSensor = rootPart:WaitForChild("GroundSensor")
local config = character:WaitForChild("Status")
local walkSpeed = config:GetAttribute("WalkSpeed")
local sprintSpeed = config:GetAttribute("SprintSpeed")
local stamina = config:GetAttribute("Stamina")

-- Get animations from ReplicatedStorage
local animations = replicatedStorage:WaitForChild("Assets"):WaitForChild("Animations"):WaitForChild("Character")

-- Set initial state and load animations
local state = "Walk"
local walkAnim = animator:LoadAnimation(animations:WaitForChild("Walk"))
local slideAnim = animator:LoadAnimation(animations:WaitForChild("Slide"))
local mantleAnim = animator:LoadAnimation(animations:WaitForChild("Mantle"))
local fallAnim = animator:LoadAnimation(animations:WaitForChild("Falling"))

-- Wait for animations to load and check for any failures
task.wait(1)
if not walkAnim or not slideAnim or not mantleAnim or not fallAnim then warn("animations failed to load") end

-- Set priorities for certain animations
mantleAnim.Priority = Enum.AnimationPriority.Action
fallAnim.Priority = Enum.AnimationPriority.Action

-- Play certain animations
local nan = 0.001
walkAnim:Play(0,nan,0)
slideAnim:Play(0,nan,0)
fallAnim:Play(0,nan,0)

-- Set up cooldowns and costs for various actions
local isSlideCooldown = true
local slideCooldown = 1
local regenCooldown = 2
local canRegen = true
local regenCoolingDown = false
local mantle = false
local sprintCost = config:GetAttribute("SprintCost")
local jumpCost = config:GetAttribute("JumpCost")
local slideCost = config:GetAttribute("SlideCost")

-- Set up parameters for raycasting
local params = RaycastParams.new()
params.FilterType = Enum.RaycastFilterType.Exclude
params.FilterDescendantsInstances = {character}

all notes were written by the assistant

it does require some tweaking because otherwise it can’t tell the difference between the variables within the blocks, so it’ll highlight multiple variables under the same purpose unless i seperate them, however I think it works fairly well.

in addition, assistant can’t exactly know what the variables’ purpose is for, so the notes aren’t really 100% accurate, but the basic annotations are fairly effective on its own.

after some testing it’s apparent that this isn’t a practical solution, as the ai likes to change or just straight up remove variables at times. and the script can’t just be copied and pasted, as the entirety of the function body is removed instead replaced with “-- function body”, which makes adding these comments just as tedious as manually writing them.

I love code that either runs on sticks, or code that is powered by the power of friendship

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