I need some thoughts/opinions on this script I’m making for a simulator game. I haven’t commented that much in this script, so feel free to ask what a certain function/piece of code does.
It would help a lot if you gave me some feedback, especially if its to organize my code even further
(This uses ProfileService btw)
--/ Services
local RunService = game:GetService("RunService")
local ServerStorage = game:GetService("ServerStorage")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")
--/ Modules
local ProfileService = require(ServerStorage.Modules.ProfileService)
--/ Instances
local EventsFolder = ReplicatedStorage:WaitForChild("RemoteEvents")
local AddClicks = EventsFolder:WaitForChild("AddClick")
local UpdateMultipliers = EventsFolder:WaitForChild("UpdateMultipliers")
local Pets = ServerStorage:WaitForChild("Pets")
--/ Varibles
local profile_template = {
Coins = 0;
Dimonds = 25;
Rebirths = 0;
Inventory = {"Example"}
}
local datastore_name = "PlayerData_346"
local ProfileStore = ProfileService.GetProfileStore(
datastore_name,
profile_template
)
local Profiles = {}
--/ Functions
local function onPlayerAdded(Player)
local profile = ProfileStore:LoadProfileAsync(
"Player_"..Player.UserId,
"ForceLoad"
)
if profile ~= nil then
profile:Reconcile()
profile:ListenToRelease(function()
Profiles[Player] = nil
Player:Kick()
end)
if Player:IsDescendantOf(Players) == true then
Profiles[Player] = profile
else
profile:Release()
end
else
Player:Kick()
end
local leaderstats = Instance.new("Folder", Player)
leaderstats.Name = "leaderstats"
local currency = Instance.new("IntValue", leaderstats)
local higherCurrency = Instance.new("IntValue", leaderstats)
local rebirths = Instance.new("IntValue", leaderstats)
currency.Name = "Coins"
higherCurrency.Name = "Dimonds"
rebirths.Name = "Rebirths"
currency.Value = profile.Data.Coins
higherCurrency.Value = profile.Data.Dimonds
rebirths.Value = profile.Data.Rebirths
local petsFolder = Instance.new("Folder", Player)
petsFolder.Name = "Pets"
for pets, pet in ipairs(profile.Data.Inventory) do -- profile.Data.Inventory has an example pet in it by default
ServerStorage.Pets:WaitForChild(pet):Clone().Parent = Player.Pets
end
end
local function onPlayerRemoved(Player)
local profile = Profiles[Player]
if profile ~= nil then
profile:Release()
print(Player.Name .. "'s profile has been released!")
end
end
local function addClicks(Player) -- For adding cash to the player
local profile = Profiles[Player]
local multiplier = 0
for index, value in ipairs(profile.Data.Inventory) do
multiplier += Pets:WaitForChild(value).Multiplier.Value
end
if multiplier == nil then
multiplier = 1
end
Player.leaderstats.Coins.Value += 1 * multiplier
profile.Data.Coins += 1 * multiplier
end
function addPets(Player, PetToAdd)
local profile = Profiles[Player]
local pet = ServerStorage.Pets[PetToAdd]:Clone()
pet.Parent = Player.Pets
table.insert(profile.Data.Inventory, PetToAdd)
end
--/ Code
for _, player in ipairs(Players:GetPlayers()) do
coroutine.wrap(onPlayerAdded)(player)
end
--/ Function Callers
Players.PlayerAdded:Connect(onPlayerAdded)
Players.PlayerRemoving:Connect(onPlayerRemoved)
AddClicks.OnServerEvent:Connect(addClicks)