I have created a depth system where it has a working depth stat and a lowest depth stat, I am trying to make the save system but I have tried multiple times and it hasn’t worked so as a result I have now to ask you.
Script:
local Players = game:GetService("Players")
Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local LowestDepth = Instance.new("IntValue")
LowestDepth.Name = "Lowest Depth"
LowestDepth.Value = 0
LowestDepth.Parent = leaderstats
local Depth = Instance.new("IntValue")
Depth.Name = "Depth"
Depth.Value = 0
Depth.Parent = leaderstats
local function DepthCheck()
if Depth.Value < LowestDepth.Value then
LowestDepth.Value = Depth.Value
end
end
Depth.Changed:Connect(DepthCheck)
--System for the depth counter using the players Y position
local function UpdateDepth()
local character = player.Character
if character then
local humanoidRootPart = character:FindFirstChild("HumanoidRootPart")
if humanoidRootPart then
local depth = humanoidRootPart.Position.Y
Depth.Value = depth
end
end
end
while true do
UpdateDepth()
wait(0.1)
end
end)
Question, do you store depth in positive or negative numbers? Because think about it like this, if you are storing lets say Depth 5, but then you reach Depth 6 then 6 < 5 will be false (6 is larger than 5) and it wont update. If you’re storing depth in negative numbers I know not what to tell you.
Okay well a good practice always is to try a print. and also, there is a chance that there’s already a player in the server before your script runs. so right around
here, add a print(“Player was added!”, player.Name) and if it doesnt fire then try
local Players = game:GetService("Players")
local function Handle(player)
print("player was handled!",player.Name)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local LowestDepth = Instance.new("IntValue")
LowestDepth.Name = "Lowest Depth"
LowestDepth.Value = 0
LowestDepth.Parent = leaderstats
local Depth = Instance.new("IntValue")
Depth.Name = "Depth"
Depth.Value = 0
Depth.Parent = leaderstats
local function DepthCheck()
if Depth.Value < LowestDepth.Value then
LowestDepth.Value = Depth.Value
end
end
Depth.Changed:Connect(DepthCheck)
--System for the depth counter using the players Y position
local function UpdateDepth()
local character = player.Character
if character then
local humanoidRootPart = character:FindFirstChild("HumanoidRootPart")
if humanoidRootPart then
local depth = humanoidRootPart.Position.Y
Depth.Value = depth
end
end
end
while true do
UpdateDepth()
wait(0.1)
end
end
Players.PlayerAdded:Connect(Handle)
for _, player in Players:GetPlayers() do
Handle(player)
end
I decided to revist this topic and here is a (hopefully) complete script :
local Players = game:GetService("Players")
local DataStore = game:GetService("DataStoreService"):GetDataStore("PlayerData")
Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local LowestDepth = Instance.new("IntValue")
LowestDepth.Name = "Lowest Depth"
LowestDepth.Value = 0
LowestDepth.Parent = leaderstats
local Depth = Instance.new("IntValue")
Depth.Name = "Depth"
Depth.Value = 0
Depth.Parent = leaderstats
local Data
local Success, ErrorMsg = pcall(function()
Data = DataStore:GetAsync(player.UserId)
end)
if Success then
if Data and Data.LowestDepth then
LowestDepth.Value = Data.LowestDepth
print("Data loaded for " .. player.Name)
end
else
warn(ErrorMsg)
end
local function DepthCheck()
if Depth.Value < LowestDepth.Value then
LowestDepth.Value = Depth.Value
end
end
Depth.Changed:Connect(DepthCheck)
--System for the depth counter using the players Y position
local function UpdateDepth()
local character = player.Character
if character then
local humanoidRootPart = character:FindFirstChild("HumanoidRootPart")
if humanoidRootPart then
local depth = humanoidRootPart.Position.Y
Depth.Value = depth
end
end
end
while true do
UpdateDepth()
wait(0.1)
end
end)
local function SaveData(Plr)
local LeaderStats = Plr.leaderstats
local Data = {
LowestDepth = LeaderStats:WaitForChild("Lowest Depth").Value
}
local Success, ErrorMsg = pcall(function()
DataStore:SetAsync(Plr.UserId, Data)
end)
if Success then
print("Data Saved")
else
warn(ErrorMsg)
end
end
Players.PlayerRemoving:Connect(SaveData)
game:BindToClose(function()
for _, Plr in pairs(Players:GetPlayers()) do
SaveData(Plr)
end
end)
I cannot believe that I forgot to add the Http service request thingy to be ON not OFF but thank you to @superlivia10alt for posting this code thanks again!