Are there any possible ways to shrink the lines of code in this?

I’ve been coding a simulator with the popular datastore module “Datastore2”

I also have problems with messy code and I would love to see the code that is actually needed!

-------------------
----- SERVICES ----
-------------------

local ReplicatedStorage = game:GetService("ReplicatedStorage")

-------------------
----- VARIABLES ---
-------------------

local Modules = ReplicatedStorage:FindFirstChild("Modules")
local DataStore2 = require(Modules:FindFirstChild("DataStore2"))

-------------------
----- FUNCTIONS ---
-------------------

game.Players.PlayerAdded:Connect(function(Player)
	
	local DataStore_Speed = DataStore2("Speed", Player)
	local DataStore_Rebirth = DataStore2("Rebirth", Player)
	
	local Board = Instance.new("Folder")
	Board.Name = "Board"
	Board.Parent = Player
	
	local Speed = Instance.new("IntValue")
	Speed.Name = "Speed"
	Speed.Parent = Board
	
	local Rebirth = Instance.new("IntValue")
	Rebirth.Name = "Rebirth"
	Rebirth.Value = 0
	Rebirth.Parent = Board
	
	local function OnSpeedUpdate(UpdatedValue)
		Speed.Value = DataStore_Speed:Get(UpdatedValue)
	end
	
	local function OnRebirthUpdate(UpdatedValue)
		Rebirth.Value = DataStore_Rebirth:Get(UpdatedValue)
	end
	
	OnSpeedUpdate(0)
	OnRebirthUpdate(0)
	
	DataStore_Speed:OnUpdate(OnSpeedUpdate)
	DataStore_Rebirth:OnUpdate(OnRebirthUpdate)
	
end)
1 Like

The code looks nice and clean. I would suggest using tables to save data so you wont need 2 data stores for Speed and Rebirth.

2 Likes

Thanks for the compliment! I tried my best to make it look good. I will look into the docs on how to save tables.

1 Like