cmodii
(cmodii)
July 9, 2020, 12:36pm
#22
I mean it’s still can be used for smaller scripts, or to show a preview of how a clean script should be for starter coders.
Other than that I never encountered an issue with this plugin, big scripts worked and looked nicer.
However, I guess you should type it out using your own method.
2 Likes
Tipic_faker
(Tipic_faker4166)
July 15, 2020, 2:23am
#23
You missed an end over here for some reasons… I see this as an absolute error.
zblox164
(zblox)
July 15, 2020, 4:23am
#24
It is meant to be there to show the difference between messy and clean code with an error.
zblox164
(zblox)
October 20, 2020, 4:01am
#25
the error is meant to be there. It shoes that cleaner code is easier to find errors than messy code.
1 Like
0Shank
(0Shank)
October 20, 2020, 7:12am
#26
zblox164:
local players = game:GetService("Players")
local dataStoreService = game:GetService("DataStoreService")
local dataStore = dataStoreService:GetDataStore("GameSaveV0.1a")
local tries = 3
local dataLoaded
local loadedData
local starterCash
-- Gets called/invoked from Get(plr)
local function Set(plr)
if dataLoaded then
-- This group is for data store related things
local key = "userCode-#plr" .. plr.UserId
local data = {
["Cash"] = starterCash,
["Level"] = 0,
["XP"] = 0
}
-- This group is used for securing the data
local count = 0
local success, err
repeat
success, err = pcall(function()
dataStore:SetAsync(key, data)
end)
count = count + 1
until success or count >= tries
if not success then
warn("Failed to set data. Error code: " .. tostring(err))
return
end
else
warn("Data has not been loaded. Please wait for data to be loaded. Plr name" .. plr.Name)
return
end
end
-- Gets invoked from CreateLeaderstats(plr) (grouping is the same as Set())
local function Get(plr)
local key = "userCode-#plr" .. plr.UserId
local count = 0
local success, err
repeat
success, err = pcall(function()
loadedData = dataStore:GetAsync(key)
end)
count = count + 1
until success or count >= tries
if not success then
warn("Failed to read data. Error code: " .. tostring(err))
plr:Kick("Failed to load data, please rejoin.")
return
end
if success then
if loadedData then
dataLoaded = true
return loadedData
else
dataLoaded = true
return {
["Cash"] = starterCash,
["Level"] = 0,
["XP"] = 0
}
end
end
end
local function CreateLeaderstats(plr)
local data = Get(plr)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = plr
local cash = Instance.new("IntValue")
cash.Name = "Cash"
cash.Parent = plr
local level = Instance.new("IntValue")
level.Name = "Level"
level.Parent = leaderstats
local xp = Instance.new("IntValue")
xp.Name = "XP"
xp.Parent = plr
cash .Value = data.Cash
level.Value = data.Level
xp.Value = data.XP
end
-- Event calls are all at the bottom (as well as a BindToClose() call)
players.PlayerAdded:Connect(CreateLeaderstats)
players.PlayerRemoving:Connect(Set)
game:BindToClose(function()
for i, v in next, players:GetChildren() do
if v then
Set(v)
end
end
end)
While your script is readable your functions aren’t versatile as they only work for leadertstats, This is a very important concept of functions, which in turn make your code more readable because now when you need to save items such as trails you don’t need to make a whole new Get() and Set() function.
here’s something I consider more versatile: first SetAsync should take in player, the data to save, and the datastore for eg:
https://gyazo.com/e254abcd4b8da308f5fe38c8b1b6768c
GetAsync should also take in the datastore:
https://gyazo.com/d96ad3f4477f644974ea397f629cf221
when GetAsync detects a new player return ‘NewPlayer’ and let the end user decide what to do:
https://gyazo.com/317027eff664775b9b068ec194aa3936
This makes your function’s versatile, sure it might take a little more setup since the function doesn’t automatically return leader stats data to you if the player is new, however the reusability it will offer in the long run will beat these 2 to 3 extra lines of code.
Rest of your points I agree with.
zblox164
(zblox)
October 20, 2020, 9:00pm
#27
Your points are true, however it is not the point of the tutorial.
0Shank
(0Shank)
October 20, 2020, 9:03pm
#28
I thought it would fit into the tutorial though since it talks about clean code, and clean code usually comes from reusable code with other factors such as indenting, comments, ect.
zblox164
(zblox)
October 20, 2020, 9:06pm
#29
Yes but, I am mainly talking about the format of your code rather then best practices when writing code. And I do agree that code should be versatile, just it is not the purpose for this tutorial.
Thanks my man, you really help newer developers with this!
1 Like