How would you set the “base” values of some players data? For example: instead of their level being “Level 0”, the level they get when they first join would inherently be “Level 1”.
Of course, I don’t want this the interfere with anything saved thereafter, I’m just uncertain as of how I would set base values to each and every set of data.
This is the code that runs upon a player joining:
game.Players.PlayerAdded:Connect(function(player)
-- // Data folder
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
-- // Player data
-- EXP//LEVEL
local Exp = Instance.new("IntValue")
Exp.Name = "Experience"
Exp.Parent = leaderstats
local Lvl = Instance.new("IntValue")
Lvl.Name = "Level"
Lvl.Parent = leaderstats
Lvl.Value = 1
local rExp = Instance.new("IntValue")
rExp.Name = "RequiredExp"
rExp.Parent = leaderstats
-- CASH
local Bal = Instance.new("IntValue")
Bal.Name = "Balance"
Bal.Parent = leaderstats
-- PLAYER STATISTICS
local Acc = Instance.new("IntValue")
Acc.Name = "Accuracy"
Acc.Parent = leaderstats
local Def = Instance.new("IntValue")
Def.Name = "Defence"
Def.Parent = leaderstats
local Health = Instance.new("IntValue")
Health.Name = "Health"
Health.Parent = leaderstats
local Sta = Instance.new("IntValue")
Sta.Name = "Stamina"
Sta.Parent = leaderstats
local Str = Instance.new("IntValue")
Str.Name = "Strength"
Str.Parent = leaderstats
local Will = Instance.new("IntValue")
Will.Name = "Will"
Will.Parent = leaderstats
local function updateHealth()
Health = (31*Lvl.Value)+469
player.Character.Humanoid.MaxHealth = Health
player.Character.Humanoid.Health = Health
print(Health)
print(player.Character.Humanoid.MaxHealth)
end
local Success, ErrMessage = pcall(function()
ExpData = ExpStore:GetAsync(player.UserId.."-Exp")
LvlData = LvlStore:GetAsync(player.UserId.."-Lvl")
rEData = rExpStore:GetAsync(player.UserId.."-rExp")
BalData = BalStore:GetAsync(player.UserId.."-Bal")
AccData = AccStore:GetAsync(player.UserId.."-Acc")
DefData = DefStore:GetAsync(player.UserId.."-Def")
HealthData = HealthStore:GetAsync(player.UserId.."-Health")
StaData = StaStore:GetAsync(player.UserId.."-Sta")
StrData = StrStore:GetAsync(player.UserId.."-Str")
WillData = WillStore:GetAsync(player.UserId.."-Will")
end)
if Success then
Exp.Value = ExpData
Lvl.Value = LvlData
rExp.Value = rEData
Bal.Value = BalData
Acc.Value = AccData
Def.Value = DefData
Health.Value = HealthData
Sta.Value = StaData
Str.Value = StrData
Will.Value = WillData
updateHealth()
else
print("Err:"..player.Name.."'s data wasn't collected")
warn(ErrMessage)
end
end)
Would I have to create (a) new line(s) of script to identify if a player has first joined to set these “base values” in place?
Nevermind, I’ve found it’s something else that’s overriding the data set in place. I joined with an alternate account and the “1” level which I had set in the code above shortly reverted to “0”.
I’m looking to set the value so it doesn’t change afterwards, but I’m not sure what’s causing it to automatically set to 0.
After a while of playing around with the code, I have figured out a solution that seems to work pretty solidly.
I first had to create a system which detected whether and player had joined already or not, which I had used this as reference for.
I updated a few of the prior lines to have “base values” which @loveicicIe had recommended prior.
From there, I forced the data to be overridden, as there would’ve been non prior, therefore setting it as the “base”
Here is the code (only including relevant parts) for those who may need it as reference:
local DataStoreService = game:GetService("DataStoreService")
local ExpStore = DataStoreService:GetDataStore("Exp")
local LvlStore = DataStoreService:GetDataStore("Lvl")
local rExpStore = DataStoreService:GetDataStore("rExp")
local FJoinStore = DataStoreService:GetDataStore("Joins")
local ExpData
local LvlData
local rEData
game.Players.PlayerAdded:Connect(function(player)
local didjoin = false
local firsttime = false
-- // Data folder
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
-- // Player data
-- EXP//LEVEL
local Exp = Instance.new("IntValue")
Exp.Name = "Experience"
Exp.Parent = leaderstats
local Lvl = Instance.new("IntValue")
Lvl.Name = "Level"
Lvl.Parent = leaderstats
Lvl.Value = 1 -- defining base value
local rExp = Instance.new("IntValue")
rExp.Name = "RequiredExp"
rExp.Parent = leaderstats
rExp.Value = 115 -- defining base value
local success, joined = pcall(function()
return FJoinStore:GetAsync(player.UserId)
end)
if success then
if joined == 1 then -- checking if value is "1", aka if player has joined before
didjoin = true
else
FJoinStore:UpdateAsync(player.UserId, 1) -- updating value to 1: first join
-- Setting the data to its "base values"
LvlStore:SetAsync(player.UserId.."-Lvl",player.leaderstats.Level.Value)
LvlData = LvlStore:GetAsync(player.UserId.."-Lvl")
rExpStore:SetAsync(player.UserId.."-rExp",player.leaderstats.RequiredExp.Value)
rEData = rExpStore:GetAsync(player.UserId.."-rExp")
firsttime = true
end
end
if not success then
print("failed")
end
local Success, ErrMessage = pcall(function()
ExpData = ExpStore:GetAsync(player.UserId.."-Exp")
LvlData = LvlStore:GetAsync(player.UserId.."-Lvl")
rEData = rExpStore:GetAsync(player.UserId.."-rExp")
end)
if Success then
Exp.Value = ExpData
Lvl.Value = LvlData
rExp.Value = rEData
else
print("Err:"..player.Name.."'s data wasn't collected")
warn(ErrMessage)
end
end)