Hey everyone! First time posting on the DevForum here.
Just as an FYI, I am more of a beginner scripter, and new modeler too. Sorry if everything looks bad …
Issue:
My leader stats script isn’t saving:
I am really unsure of why its not saving. It saved when I first only had the “Cash” value. I added “Diamonds” and to be completely honest, I wasn’t super sure what I was doing for making that save too. Here is the script:
local DataStoreService = game:GetService("DataStoreService")
local myDataStore = DataStoreService:GetDataStore("myDataStore")
game.Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local Cash = Instance.new("IntValue")
Cash.Name = "Cash"
Cash.Parent = leaderstats
local Diamonds = Instance.new("IntValue")
Diamonds.Name = "Diamonds"
Diamonds.Parent = leaderstats
local playerUserId = "Player"..player.UserId
local data
local success, errormessage = pcall(function()
data = myDataStore:GetAsync(playerUserId)
end)
if success then
Cash.Value = data--loads data when player joins
Diamonds.Value = data
end
while wait(1) do
player.leaderstats.Cash.Value = player.leaderstats.Cash.Value + 1
player.leaderstats.Diamonds.Value = player.leaderstats.Diamonds.Value + 1
end
end)
game.Players.PlayerRemoving:Connect(function(player)
local playerUserId = "Player"..player.UserId
local data1 = player.leaderstats.Cash.Value
local data2 = player.leaderstats.Diamonds.Value
myDataStore:SetAsync(playerUserId, data1, data2) --saves data when player leaves game
end)
Thank you to all in advance! I look forward to the help!