Hello. I’m trying to figure out how to change and add to a players “stats” with in a DataBase, but I don’t seem to be making progress, probably because me as well as my code is all over the place, let me explain what I’ve done.
This is the player data being created/found:
local DataStoreService = game:GetService("DataStoreService")
local Data = DataStoreService:GetDataStore("Data")
local MessagingService = game:GetService("MessagingService")
local Players = game:GetService("Players")
local e = require(game:GetService("ServerStorage").Functions)
Players.PlayerAdded:Connect(function(player)
local PlayerData = Instance.new("Folder", player)
PlayerData.Name = "PlayerData"
local LevelCompletions = Instance.new("Folder", PlayerData)
LevelCompletions.Name = "LevelCompletions"
local L0 = Instance.new("IntValue", LevelCompletions)
L0.Name = "Level 0 Completions"
local L1 = Instance.new("IntValue", LevelCompletions)
L1.Name = "Level 1 Completions"
local L0Data = nil
local L1Data = nil
local success, err = pcall(function()
L0Data = Data:GetAsync(tostring(player.UserId), L0)
L1Data = Data:GetAsync(tostring(player.UserId), L1)
end)
if success then
L0.Value = L0Data
L1.Value = L1Data
print("Successfully Grabbed "..player.Name.."'s Data!")
else
warn(err)
end
end)
This is the function that is called when the server is invoked that is responsible for adding 1 to a IntValue of theirs:
local functions = {}
function functions.UpdateData(player)
local DataStoreService = game:GetService("DataStoreService")
local Data = DataStoreService:GetDataStore("Data")
local PlayerData = player.PlayerData
local L0 = player.PlayerData.LevelCompletions["Level 0 Completions"].Value
local L1 = player.PlayerData.LevelCompletions["Level 1 Completions"].Value
local L0Data = nil
local L1Data = nil
L0Data = Data:GetAsync(tostring(player.UserId), L0)
L1Data = Data:GetAsync(tostring(player.UserId), L1)
Data:UpdateAsync(player.UserId, function(Data)
L0Data = L0 + 1;
end)
print(player.Name)
end
return functions
If I need to explain or elaborate more on what I’m trying to accomplish, I will.
I’d recommend you create a folder [ for all levels in your game] and put it inside the player when he joins your game, now, you have several ways to continue from here.
One way is to check whenever the player has leveled/completed a mission, and once he’s done, fire a remote [ if its from the client] to the server, which will create a Boolean into your folder, and since he’s completed it, you’d set its value to true. [name those booleans as your mission names, it’d help later on]
Now, you can save the player’s folder and booleans and reload them with their value when the player has joined again [ and he has data],
and with that, you can check for every player if he has completed a mission, and if he did, dont let him do it again [ by checking its value on the folder].
Well, I plan on letting the player redo the level as many times as they’d like.
The other thing is, why can’t I do it the way I have done it so far? (which is having one folder, containing all IntValues for Level Completions inside of that?
If you want them to do that mission for how many times they want, one way is to add an int value inside of each ‘Level Completed’ value, name it 'TimesCompleted, and for each time the player has completed it, just add to that +1
You can update their states outside of the data script,
if I understood you right, and you want to change the player’s mission value, you can do it on another script , while you access to that folder and those values accordingly.