How can I optimize my code and make it less chaotic?

I want this code to be less chaotic, and also shorter (if possible) The reason for this is because I want it to be access and easily be able to be read by my friends helping on the project
(sorry for all the typos)

local DataStoreService = game:GetService("DataStoreService")
local Players = game:GetService("Players")

local dataStore = DataStoreService:GetDataStore("PlayerDataStore")

game.Players.PlayerAdded:Connect(function(plr)
	local leaderstats = Instance.new("Folder")
	leaderstats.Parent = plr
	leaderstats.Name = "leaderstats"

	local coins = Instance.new("IntValue")
	coins.Parent = leaderstats
	coins.Name = "Coins"

	local rebirths = Instance.new("IntValue")
	rebirths.Parent = leaderstats
	rebirths.Name = "Rebirths"

	-- Attempt to load data
	local success, data = pcall(function()
		return dataStore:GetAsync(tostring(plr.UserId))
	end)

	if success and data then
		coins.Value = data.Coins or 0
		rebirths.Value = data.Rebirths or 0
	end
end)

game.Players.PlayerRemoving:Connect(function(plr)
	-- Save data when the player leaves
	local success, error = pcall(function()
		dataStore:SetAsync(tostring(plr.UserId), {Coins = plr.leaderstats.Coins.Value, Rebirths = plr.leaderstats.Rebirths.Value})
	end)
	if not success then
		warn("Data failed to save for " .. plr.Name .. ": " .. error)
	end
end)

Thanks for helping if you can!

3 Likes

Instance.new can also take Parent as a second parameter,
local coins = Instance.new("IntValue", leaderstats)

besides this i don’t know maybe others can help

I completly forgot this existed, thanks!

edit: Kinda dumb that the Instance.new("") doesn’t display a parent value when looking at it-
image
It’s really stupid, as you see above it only tells you the className as the first parmienter…instead of showing the second parent perimeter value
(no offense)

2 Likes

its always worth spotting repeatable code: here you have 3 new instances, setting the same properties. So create a function called something like CrateInstance and pass it some parameters for the properties.

local function CreateInstance(type,name,parent)
   local returnValue = Instane.new(type)
   returnValue.Name=name
   returnValue.Parent=parent
   return returnValue
end

then inside the connection funtion you can use the above function

local leaderstats = CreateInstance("Folder","leaderstats",plr)
local coins = CreateInstance("IntValue","coins",leaderstats)
local rebirths  = CreateInstance("IntValue","rebirths",leaderstats)
1 Like

Thanks, that is defiantly a smart thing for me to learn/do!

2 Likes

Oh, I should not use that then-

Thanks!

2 Likes

At the start of your code, you already get the Players service:

Although, for the rest of your code, it doesn’t look like you used the variable Players:

1 Like

There are so many ways to re look at something, again relooking at it you could pass a table with the params into the CreateInstance rather than 3 seperate params. But generally i prefer seperate values and params unless it gets to above 3 then i pass a table with the variables and values inside the table :slight_smile: Its great to revisit code sometimes and see things in a completly new way, very creative :slight_smile: keep it up. Also if i cant read the complete function on the screen wihtout scrolling, then split into smaller functions called by the main one etc, etc. you get the idea

1 Like

I forgot I created that line from a old script- thx!

2 Likes

I would personally leave a blank between each lines

when ever I encounter a function or a major section I will leave 3 or 5 blanks lines so that you can see the major parts of the code.

Remember to use functions

This example I shown is around 1500 lines, which is totally impossible to acheive without this habit. My script was totally trash before I realized to code like this. (Even it’s just a hundred line)

The parent argument was “deprecated” since oh no replication and you wait like 2 milliseconds for Roblox to also start stuff. Basically, if you don’t change many properties you’re fine.

The second parameter can cause memory leaks or spikes.

I’m confused cause there is a fight happening about it, Is it smart to use it not?

You should never use the second parameter in Instance.new() ever.

That helped, Cause people told me so. Thanks for solving it

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