Disclaimer: There are no bugs, just need a bit of guidance moving forward
Sorry if this is the wrong category, if it is please let me know and I’ll change it ASAP.
This is a relatively long post, so if you’re invested go ahead and read.
I recently switched over to Visual Studio Code to work on my new game. I have installed Knit and ProfileService. As I begin this project I just want to know if this is the correct way to proceed, since it’s my first time using the Knit framework.
Current Explorer:
Not sure why but I always start with the data, but inside src > server > Services
I have a module script called DataService. I debated whether this would be a service or component since this is the script that will create/ handle their data.
The “DataService.lua” script. It’s quite long but it’s mainly based on the ProfileService Documentation.
-- Services
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")
-- Modules
local Knit = require(ReplicatedStorage:WaitForChild("Common"):WaitForChild("Knit"))
local UserData = require(script.Parent.Parent:WaitForChild("Module"):WaitForChild("DataTable"))
local ProfileService = require(ReplicatedStorage:WaitForChild("Common"):WaitForChild("ProfileService"))
local DataService = Knit.CreateService{
Name = "DataService",
Client = {},
}
local ProfileStore = ProfileService.GetProfileStore(
"PlayerData",
UserData
)
local Profiles = {}
function CreateProfile(player)
local profile = ProfileStore:LoadProfileAsync("Player_"..player.UserId)
if profile then
profile:Reconcile() -- Fills missing variables from UserData
profile:ListenToRelease(function() -- If profile is loaded in another Roblox Server
Profiles[player] = nil
player:Kick()
end)
if player:IsDescendantOf(Players) then
Profiles[player] = profile -- Profile Loaded
else
profile:Release() -- Player Left before their profile loaded
end
else
player:Kick() -- Another server was loading the profile at the same time
end
end
function RemoveProfile(player)
local profile = Profiles[player]
if profile then
profile:Release()
end
end
function DataService:KnitInit()
-- Player Events
Players.PlayerAdded:Connect(CreateProfile)
Players.PlayerRemoving:Connect(RemoveProfile)
-- Safety
for _, player in ipairs(Players:GetPlayers()) do -- Just incase player spawns before script runs
task.spawn(CreateProfile, player)
end
game:BindToClose(function() -- If game closes/ Shuts down
for _, player in ipairs(Players:GetPlayers()) do
task.spawn(CreateProfile, player)
end
end)
end
function DataService:KnitStart()
-- Not sure if this is needed :/
end
return DataService
My questions are:
Are there any bad habits or things I should remove or refrain from doing?
Let’s say I start creating functions like AddCoins
or AddToInv
would I do it in this script or components folder?
Is this the best way to move forward?
Any tips/ suggestions?
Extra stuff on my mind
I was debating on whether I should post this since I know it will get mixed reactions, but I’ve been sitting here for hours just looking reading through others’ posts. The annoying thing is that I’m a visual learner, so reading documents sometimes doesn’t make sense whereas if you show me a visual representation, I would. Just to end, I would like to say thank you for reading this all, I really want to carry on scripting and feel like I’m asking stupid questions like this. Appreciate your time and help!