While using the Developer Hub on scripting a DataStore, i’ve encountered , besides an error on saving data, some different errors which didn’t appeared before i made the datastore. What i want to do is to save a player’s money, level and items (if he buys them) in a datastore and , as i said, i got extremely worried about the amount of errors i got from one script.
Anyway here is some information about my problem
Here are the errors i mentioned above. As you can see i get the errors that my data didn’t load.
The script i used DataStore in, so i can save a player’s money , script situated in ServerScriptService
local playerDSS = DataStoreService:GetDataStore("DSS")
game.Players.PlayerAdded:Connect(function(player)
local stats = Instance.new("Folder", player)
stats.Name = "leaderstats"
local vall = Instance.new("IntValue", stats)
vall.Name = "Cash"
vall.Value = 0
spawn(function()
while true do
vall.Value = vall.Value + 1
wait(60)
end
end)
vall:GetPropertyChangedSignal("Value"):Connect(function() --Function will run every time Value is changed, meaning either an increase or decrease in cash.
local cashNumber = player.PlayerGui:WaitForChild("MenuInterface"):WaitForChild("Menu"):WaitForChild("ProfileButton"):WaitForChild("ProfileMainFrame"):WaitForChild("SecFrame"):WaitForChild("CashNumber")
cashNumber.Text = vall.Value
end)
local data
local success,errormessage = pcall(function()
data = playerDSS:GetAsync(player.UserId.."-cash")
end)
if success then
cash.Value = data
else
print("Error loading data!")
warn(errormessage)
end
end)
game.Players.PlayerAdded:Connect(function(player)
local success, errormessage = pcall(function()
playerDSS:SetAsync(player.UserId.."-cash",player.leaderstats.Cash.Value)
end)
if success then
print("Data successfuly saved!")
else
print("Error saving data!")
warn(errormessage)
end
end)
I did what you and @Snoopy1333 said and i still get these errors
Also i noticed that “cash” in my script is unknown. I tried moving the whole function around and cash is still unknown.
local Ds = game:GetService("DataStoreService"):GetDataStore("playerDSS")
game.Players.PlayerAdded:Connect(function(player)
local stats = Instance.new("Folder", player)
stats.Name = "leaderstats"
local vall = Instance.new("IntValue", stats)
vall.Name = "Cash"
vall.Value = 0
spawn(function()
while true do
vall.Value = vall.Value + 1
wait(60)
end
end)
vall:GetPropertyChangedSignal("Value"):Connect(function() --Function will run every time Value is changed, meaning either an increase or decrease in cash.
local cashNumber = player.PlayerGui:WaitForChild("MenuInterface"):WaitForChild("Menu"):WaitForChild("ProfileButton"):WaitForChild("ProfileMainFrame"):WaitForChild("SecFrame"):WaitForChild("CashNumber")
cashNumber.Text = vall.Value
end)
local data
local success,errormessage = pcall(function()
data = Ds:GetAsync(player.UserId.."-cash")
end)
if success then
cash.Value = data
else
print("Error loading data!")
warn(errormessage)
end
end)
It says that “cash” is unknown , in cash.Value = data
Well, because cash as a variable simply isn’t declared in that anonymous function. - But vall is (<-- big hint ).
BTW is it discouraged to use the second argument for Instance.new(). Instead set the .Parent much later, as can be seen in this code sample.
Also you should probably avoiding this infinite, never-ending-loop-that-stops-only-when-it-crashes-due-to-error, code:
spawn(function()
while true do
vall.Value = vall.Value + 1
wait(60)
end
end)
Perhaps instead make a RunService, Heartbeat function, that does the “increment cash by 1” for all the players still connected to the game.
local Players = game:GetService("Players")
local RunService = game:GetService("RunService")
local sumTime = 0
local secondsBetweenCashIncrement = 60
RunService.Heartbeat:Connect(function(dt)
sumTime = sumTime + dt
if sumTime > secondsBetweenCashIncrement then
sumTime = sumTime - secondsBetweenCashIncrement
for _,ply in ipairs(Players:GetChildren()) do
local leaderstats = ply:FindFirstChild("leaderstats")
if leaderstats then
local cash = leaderstats:FindFirstChild("Cash")
if cash then
cash.Value = cash.Value + 1
end
end
end
end
end)
I watched the video Snoopy1333 showed me and it works just fine but im also thankful to Decker004 for explaining me so much.
With the video @Snoopy1333 showed me and with what @Decker004 told me about increment cash by 1 script i finally managed to save the data after struggling 2 days to understand it from youtube videos and Developer Hub. Also, thank you so much @FlashFlame_Roblox for being here again to help me, since i realised that you’ve helped me in my other topics posted this week.
Even tho Decker and FlashFlame helped me too, i’d give the solution to Snoopy’s video, which helped me understand how DataStore works.