hey yall
So I was trying a datastore I made until I ran into a problem.
When I add 10 Studs it goes back to 0 and (maybe) does not save!
I don’t know what to do so now I am here.
Script
--- Add Leaderstats ---
game:GetService("Players").PlayerAdded:Connect(function(Player)
local Folder = Instance.new("Folder", Player)
Folder.Name = "leaderstats"
local NumberValue = Instance.new("NumberValue", Folder)
NumberValue.Name = "Studs"
end)
--- Add Studs/minute and Save Currency ---
game:GetService("Players").PlayerAdded:Connect(function(Player)
local NumberValue = Player:WaitForChild("leaderstats").Studs
local DataStore = game:GetService("DataStoreService")
local GameDataStore = DataStore:GetDataStore("GameDataStore")
local Data
if Player.MembershipType == Enum.MembershipType.None then
while wait(60) do
Player.leaderstats.Studs.Value += 5
wait(1)
local Success, ErrorMessage = pcall(function()
Data = GameDataStore:GetAsync(Player.UserId.."-NumberValue")
end)
if Success then
NumberValue.Value = Data
else
local GUI = Player:WaitForChild("PlayerGui").DataSaveFailed
GUI.TextLabel.Visible = true
warn("Saving for player ||"..Player.Name.."|| failed!")
end
end
elseif Player.MembershipType == Enum.MembershipType.Premium then
while wait(30) do
Player.leaderstats.Studs.Value += 10
wait(1)
local Success, ErrorMessage = pcall(function()
Data = GameDataStore:GetAsync(Player.UserId.."-NumberValue")
end)
if Success then
NumberValue.Value = Data
else
local GUI = Player:WaitForChild("PlayerGui").DataSaveFailed
GUI.TextLabel.Visible = true
warn("Saving for player ||"..Player.Name.."|| failed!")
end
end
end
end)
--- Save Currency When Player Is Leaving ---
game:GetService("Players").PlayerRemoving:Connect(function(Player)
local NumberValue = Player.leaderstats.Studs
local DataStore = game:GetService("DataStoreService")
local GameDataStore = DataStore:GetDataStore("GameDataStore")
local Success, ErrorMessage = pcall(function()
GameDataStore:SetAsync(Player.UserId.."-NumberValue", Player.leaderstats.Studs.Value)
end)
if Success then
print("Data for player ||"..Player.Name.."|| was saved succesfully!")
else
warn("Saving data upon leaving for player ||"..Player.Name.."|| failed!")
end
end)
Thank you! Waiting for a reply.
Attempt 2