#New to scripting, looking for feedback.

1 messages · Page 1 of 1 (latest)

violet rain
#

Hey, I am new to scripting and I have been toying around with some stuff, I created a simple cash system using tools and leaderstats, just looking for feedback on it and maybe some improvements. (not stuff to add, how to improve the script in efficiency and such):

local leaderstats = game.Players.LocalPlayer:WaitForChild("leaderstats")
local Cash = leaderstats:WaitForChild("Cash")

local player = game.Players.LocalPlayer
local CashGUI = player.PlayerGui.CashGUI
local CashLabel = CashGUI.CashLabel

local cashAmount = Cash.Value

-- Update cash display
local function formatCash(cashAmount)
    if cashAmount >= 1e27 then
        return string.format("%.2fOc", cashAmount / 1e27)
    elseif cashAmount >= 1e24 then
        return string.format("%.2fSp", cashAmount / 1e24)
    elseif cashAmount >= 1e21 then
        return string.format("%.2fSx", cashAmount / 1e21)
    elseif cashAmount >= 1e18 then
        return string.format("%.2fQi", cashAmount / 1e18)
    elseif cashAmount >= 1e15 then
        return string.format("%.2fQa", cashAmount / 1e15)
    elseif cashAmount >= 1e12 then
        return string.format("%.2fT", cashAmount / 1e12)
    elseif cashAmount >= 1e9 then
        return string.format("%.2fB", cashAmount / 1e9)
    elseif cashAmount >= 1e6 then
        return string.format("%.2fM", cashAmount / 1e6)
    elseif cashAmount >= 1e3 then
        return string.format("%.2fK", cashAmount / 1e3)
    else
        return tostring(cashAmount)
    end
end

local formattedAmount = formatCash(cashAmount)
local cashPrefix = "$"

task.spawn(function()
    while true do
        task.wait(0.1)
        CashLabel.Text = cashPrefix..formatCash(cashAmount)
    end
end)

local StarterPack = game:GetService("StarterPack")
local giveCash

-- Cash Tool Variables:
local lowCash = StarterPack.lowCash
local highCash = StarterPack.highCash
local insaneCash = StarterPack.insaneCash
local superCash = StarterPack.superCash
local wtfCash = StarterPack.wtfCash

-- Cash Tool Setup:
local function setup(char)
    char.ChildAdded:Connect(function(child)
        if child:IsA("Tool") then
            if child.Name == "cashResetter" then
                cashAmount = 0
                Cash.Value = 0
            end
            local function cashGiver(name, amount)
                if child.Name == name then
                    while task.wait(0.1) do
                        cashAmount += amount
                        Cash.Value += amount
                    end
                end
            end
            print("Equipped", child.Name)
            giveCash = task.spawn(function()
                cashGiver(lowCash.Name, 150)
                cashGiver(highCash.Name, 3.25e3)
                cashGiver(insaneCash.Name, 12.5e3)
                cashGiver(superCash.Name, 5.25e7)
                cashGiver(wtfCash.Name, 725e12)
            end)
        end
    end)
    
    char.ChildRemoved:Connect(function(child)
        if child:IsA("Tool") then
            print("Unequipped", child.Name)
            task.cancel(giveCash)
        end
    end)
end

player.CharacterAdded:Connect(setup)

if player.Character then
    setup(player.Character)
end

I also provided a video to show what it actually does.

violet rain
ocean silo
#

i can help

violet rain
#

oh yeah and if anyone feels like it, heres a slightly older script if ur bored and wanna read / review

ocean silo
#

i can help you script

#

what do you know so far?

violet rain
violet rain
ocean silo
#

do you know how to print hello in the output?

violet rain
#

functions, function nesting, while loops, task.spawn, tool detection (ChildAdded/Removed), LocalScripts vs StarterGui vs PlayerGui, leaderstats, ValueObjects, RemoteFunctions, string.format, scientific notation (1e3 etc), UI updating, infinite loop blocking, scope, variable naming, cash formatting (K/M/B/etc), event connections, and tool-based cash generation systems.

#

thats the stuff i know i know idk about anything else

violet rain
ocean silo
#

well that's pretty much all you need

#

the stuff you gave me that you know

#

all you need is data store and you should be good to go

#

also module scripts

violet rain
#

Nah I know scripting ain’t learnable in 2 weeks

#

It takes years I’m barely in the beginner stage

violet rain
ocean silo
#

but you said your new 🤨

violet rain
#

I am

violet rain
smoky storm
# violet rain Hey, I am new to scripting and I have been toying around with some stuff, I crea...

That if-else chain is positively evil

Since we know each suffix increases by a multiple of 1000, we can store all of them in a table and then do a lookup. Something like this:

local suffixes = {"", "K", "M", "B", "T", "Qa", "Qi", "Sx", "Sp", "Oc"}
local function formatCash(cashAmount)
  for i, suffix in suffixes do
    local new = cashAmount / 1000^(i-1)
    if new < 1000 or i == #suffixes then
      return string.format("%.2f%s", new, suffix)
    end
  end
end

-# There might be a way to do it non-itterativly, with just math, but I didn't want to put the effort in to figure that out.

atomic jungle
# violet rain Hey, I am new to scripting and I have been toying around with some stuff, I crea...

prolly best to use a consistent naming case (preferably camelCase) and keep all your variables initialised at the top
instead of a polling for cashprefix, use getPropertyChangedSignal
use a lookup table instead of elseifs
use guard clauses instead of nesting so:

if not child:IsA("Tool") then 
    return 
end

-- code

prolly best to not define functions inside events cos of memory leaks and just readability in general

smoky storm
atomic jungle
#

there shouldnt be any leaks in this case tho

smoky storm
#

In what situation would you get a memory leak from defining a function inside of an event?

violet rain
#

I am a beginner you realise

#

no clue what none of what u j said means

atomic jungle
atomic jungle
# violet rain 🥀

search up what camelCase is and use for naming ur variables

create ur variables at the top of the script

theres an event called getPropertyChangedSignal which u should use instead of a while loop for the cashprefix

instead of nesting like below, u should use the latter, more readable and cleaner

if cond then
— code
end
u can do

if not cond then
return
end
— code goes here

atomic jungle
violet rain
#

I choose what I think looks nice to me lmfao

atomic jungle
violet rain
#

what

#

speak english

atomic jungle
#

check msg

violet rain
#

guard santa

#

got it

atomic jungle