I was the one the recommended ProfileService since it is really useful. I will show you how to use it:
First download the module and put into ServerStorage or as a child of the data script.
Next make the data I would create a table called data
local Data = {
Cards = {},
Money = 0,
-- Other Values
}
That is really all you need for the data.
Now for the main script. This is what it would look like:
--// Services
local Players = game:GetService("Players")
--// Profile
local ProfileService = require(Where.You.Saved.The.Module)
-- For this you want a key and the data template. The key were using is "PlayerData" and the Data will be the table we made
local ProfileStore = ProfileService.GetProfileStore("PlayerData", Data)
local Profiles = {} -- This will the store the players session data
local function PlayerAdded(player) -- This function will run when player gets added
local profile = ProfileStore:LoadProfileAsync("Player_" .. player.UserId) -- Get's the players data
if profile then -- If profile exist
profile:AddUserId(player.UserId) -- GDPR compliance
profile:Reconcile() -- This is optional but it will fill in the missing variables. If you update data when players already have data it will update their data to fill in the gaps
profile:ListenToRelease(function() -- This is needed due to the way profileService works
Profiles[player] = nil
-- The profile could've been loaded on another Roblox server:
player:Kick()
end)
if player:IsDescendantOf(Players) then
Profiles[player] = profile
-- A profile has been successfully loaded:
-- Their Data has loaded and now you can do what you want with it
else
-- Player left before the profile loaded:
profile:Release()
end
else
player:Kick("Couldn't load profile")
end
end
-- In case Players have joined the server earlier than this script ran:
for _, player in ipairs(Players:GetPlayers()) do
task.spawn(PlayerAdded, player)
end
----- Connections -----
Players.PlayerAdded:Connect(PlayerAdded)
Players.PlayerRemoving:Connect(function(player)
local profile = Profiles[player]
if profile then
profile:Release()
end
end)
As you can see this is how the basic script would look like, once you understand it, it will be a lot easier to add stuff to it. Now the best part about this is you can really use this in any game simply by copy and pasting since everything is set the only thing you will need to update is the data.
Now that we have that we want to actually save the cards. For this we will have a function.
local function AddCardToData(player: Player)
local Profile = Profiles[player]
local CardToAdd = {
Name = "Card Name Here",
Rarity = "Common",
Strength = 2,
Exp = 100, -- If you have EXP there's no need for level really
Resistence = 2,
-- There is no need for the owned variable since if it's in their data they already own it
}
-- Add it to the players Cards data
table.insert(Profile.Data.Cards, CardToAdd)
end
As you can see that is a function that will add the cards to the player data and you can already see some issues. Whenever this function is called it will always add the same card. So for this we can add ... to the function and for this you will have to send the variables in order so, Name, Rarity, Strength, Exp, Resistence
local function AddCardToData(player: Player, ...)
local Profile = Profiles[player]
local Args = {...} -- This will look something like this:
-- Args = {[1] = "Card Name", [2] = "Rare", [3] = 5, etc}
local CardToAdd = {
Name = Args[1],
Rarity = Args[2],
Strength = Args[3],
Exp = Args[4], -- If you have EXP there's no need for level really
Resistence = Args[5],
-- There is no need for the owned variable since if it's in their data they already own it
}
-- Add it to the players Cards data
table.insert(Profile.Data.Cards, CardToAdd)
end
-- Now whenever we want to add a card to the players table all we need to is
AddCardToData(player, "Card 2", "Common", 3, 500, 6)
-- Name: Card 2
-- Rarity: Common
-- Strength : 3
-- Exp : 500
-- Resistence : 6
Quite simple, sorry I couldn’t explain it with normal datastore since I haven’t used it in a while but let me know what you think. This is the whole script and you can simple just copy and paste it and it should work as long as you put the module in place. I haven’t tested it.
TL:DR: This is the whole script using profileservice. Hope this helps.
local Data = {
Cards = {},
Money = 0,
-- Other Values
}
--// Services
local Players = game:GetService("Players")
--// Profile
local ProfileService = require(Where.You.Saved.The.Module)
-- For this you want a key and the data template. The key were using is "PlayerData" and the Data will be the table we made
local ProfileStore = ProfileService.GetProfileStore("PlayerData", Data)
local Profiles = {} -- This will the store the players session data
local function PlayerAdded(player) -- This function will run when player gets added
local profile = ProfileStore:LoadProfileAsync("Player_" .. player.UserId) -- Get's the players data
if profile then -- If profile exist
profile:AddUserId(player.UserId) -- GDPR compliance
profile:Reconcile() -- This is optional but it will fill in the missing variables. If you update data when players already have data it will update their data to fill in the gaps
profile:ListenToRelease(function() -- This is needed due to the way profileService works
Profiles[player] = nil
-- The profile could've been loaded on another Roblox server:
player:Kick()
end)
if player:IsDescendantOf(Players) then
Profiles[player] = profile
-- A profile has been successfully loaded:
-- Their Data has loaded and now you can do what you want with it
else
-- Player left before the profile loaded:
profile:Release()
end
else
player:Kick("Couldn't load profile")
end
end
local function AddCardToData(player: Player, ...)
local Profile = Profiles[player]
local Args = {...} -- This will look something like this:
-- Args = {[1] = "Card Name", [2] = "Rare", [3] = 5, etc}
local CardToAdd = {
Name = Args[1],
Rarity = Args[2],
Strength = Args[3],
Exp = Args[4], -- If you have EXP there's no need for level really
Resistence = Args[5],
-- There is no need for the owned variable since if it's in their data they already own it
}
-- Add it to the players Cards data
table.insert(Profile.Data.Cards, CardToAdd)
end
-- In case Players have joined the server earlier than this script ran:
for _, player in ipairs(Players:GetPlayers()) do
task.spawn(PlayerAdded, player)
end
----- Connections -----
Players.PlayerAdded:Connect(PlayerAdded)
Players.PlayerRemoving:Connect(function(player)
local profile = Profiles[player]
if profile then
profile:Release()
end
end)
I really hope this helps it did take some time to write this so hopefully, it is useful. Let me know if you need any help and also try not to make multiple posts on the same subject instead just use one until you find a solution. Have a good day!