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)
edit: Kinda dumb that the Instance.new("") doesn’t display a parent value when looking at it-
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)
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)
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 Its great to revisit code sometimes and see things in a completly new way, very creative 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
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.