Hello there, I’m working on a new game that requires a leaderstats + a datastore for the leaderstats. The only issue is that I am not a scripter so for me to complete this script I had to watch and spend 50 minutes making this.
My question is… Is this script as short as it can be, and is the script well scripted as I don’t wanna have issues down the road by player’s data resetting.
Script:
local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetDataStore("DataStore")
game.Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new ("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local Coins = Instance.new("IntValue")
Coins.Name = "Coins"
Coins.Parent = leaderstats
local Wins = Instance.new("IntValue")
Wins.Name = "Wins"
Wins.Parent = leaderstats
local playerUserId = "Player_"..player.UserId
local data
local success, errormessage = pcall(function()
data = DataStore:GetAsync(playerUserId)
end)
if success then
if data then
Coins.Value = data.Coins
Wins.Value = data.Wins
end
end
end)
game.Players.PlayerRemoving:Connect(function(player)
local playerUserId = "Player_"..player.UserId
local data = {
Coins = player.leaderstats.Coins.Value;
Wins = player.leaderstats.Wins.Value;
}
local success, errormessage = pcall(function()
DataStore:SetAsync(playerUserId, data)
end)
if success then
print("Data successfully saved.")
else
print("There was an error while saving!")
warn("errormessage")
end
end)
I am going to assume it uses the Roblox Leaderboard, as a composer and not a very good scripter, I can’t really find a good way of making it short, the store seems good as well, on the fact that you are using ID’s, I cant see any problems down the road as well (unless for some reason DataStores all get reset).
Alright, thank you. I only wanted it to make it short as I am gonna have 1 script that controls my whole game as I heard it’s better than having tons and tons of single scripts.
I would highly suggest against this, if one line errors all the code after that line won’t run. Make a separate script for every piece of code, and modules when needed. Roblox let’s you have multiple scripts for a reason, use them!
I honestly don’t know, could you provide some places where people were using a single script? They might be doing it to keep all the local variables and functions without making a bunch of module scripts or something. They could also have done that to keep their explorer neat, although a messy explorer is fine as long as you have a functioning game!
Also, when I say multiple I don’t mean hundreds, more like 35 or so. Just group stuff together in scripts, like all stuff for stats in one script and all stuff for a shop in one script.