Hello, I’m creating a game and I’m new to scripting so I followed a youtube tutorial for this.
I copied the whole script word for word (except changing Cash to Money)
local moneyDataStore = DataStoreService:GetDataStore("Money")
game.Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder", player)
leaderstats.Name = "leaderstats"
local Money = Instance.new("IntValue", leaderstats)
Money.Name = "money"
Money.Value = 0
local playerUserId = "player_"..player.UserId
--lOADING moni DATA
local MoneyData
local success, errormessage = pcall(function()
MoneyData = moneyDataStore:GetAsync(playerUserId)
end)
if success then
Money.Value = MoneyData
end
end)
game.Players.PlayerRemoving:Connect(function(player)
local playerUserId = "player_"..player.UserId
--saving money
local MoneyValue = player.leaderstats.Money.Value
local success, errormessage = pcall(function()
moneyDataStore:SetAsync(playerUserId, MoneyValue)
end)
end)
game:BindToClose(function()
for _, player in pairs(game.Players:GetPlayers()) do
local playerUserId = "player_"..player.UserId
--saving money
local MoneyValue = player.leaderstats.Money.Value
local success, errormessage = pcall(function()
moneyDataStore:SetAsync(playerUserId, MoneyValue)
end)
end
end)
Unfortunately, when I set the money value to 100 and rejoin, it doesnt stay.
Anyone know why? (Datastores are enabled in game settings.)
Using ChatGPT, a progamming robot that can help answer programming problems, here are some potential issues it hightlighted with your code:
"
There are a couple of things you can check to help identify the issue you’re experiencing:
Make sure that you have the correct spelling and capitalization for the DataStore and the leaderstats folder. In this case, the DataStore is named “Money” and the leaderstats folder is created as a child of the player object, so they should be spelled with capital M and F.
Check the output in the Output window for any error messages that might provide more information about the issue. This can help you understand what might be going wrong and how to fix it.
Make sure that you have correctly enabled DataStores in your game settings. To do this, go to the Game Settings page in the Roblox Studio, click the “Advanced” tab, and make sure that the “Allow Data Stores” option is checked.
Check that you are properly saving and loading the money value. The script you posted appears to be saving and loading the money value correctly, but it’s possible that there is an issue with how you are setting the initial value or how you are using the value in your game. Make sure that you are setting the value of the Money IntValue correctly and that you are using the correct value when you want to access the player’s money.
"
Yes you should change it to a PlayerRemoving function and see if that works. If the game closes unexpectedly, then the function will never fire for the BindToClose function.
Are you changing the money value server side or just locally? If you are changing it locally, the value will never save because the server will not see any changes (Client changes are not replicated to server)
He is using PlayerRemoving and BindToClose. BindToClose is to save player data if the server shuts down. Otherwise if the server shuts down, nobody’s data would save.
Here is some code that may work for your particular issue. Taken from ChatGPT an AI programming robot:
local DataStoreService = game:GetService("DataStoreService")
local moneyDataStore = DataStoreService:GetDataStore("Money")
game.Players.PlayerAdded:Connect(function(player)
-- Create leaderstats folder and Money value for the player
local leaderstats = Instance.new("Folder", player)
leaderstats.Name = "leaderstats"
local Money = Instance.new("IntValue", leaderstats)
Money.Name = "money"
Money.Value = 0
-- Load player's money data from the DataStore
local playerUserId = "player_"..player.UserId
local MoneyData
local success, errormessage = pcall(function()
MoneyData = moneyDataStore:GetAsync(playerUserId)
end)
if success then
Money.Value = MoneyData
end
end)
game.Players.PlayerRemoving:Connect(function(player)
-- Save player's money data to the DataStore
local playerUserId = "player_"..player.UserId
local MoneyValue = player.leaderstats.Money.Value
local success, errormessage = pcall(function()
moneyDataStore:SetAsync(playerUserId, MoneyValue)
end)
end)
game:BindToClose(function()
-- Save money data for all players when the game closes
for _, player in pairs(game.Players:GetPlayers()) do
local playerUserId = "player_"..player.UserId
local MoneyValue = player.leaderstats.Money.Value
local success, errormessage = pcall(function()
moneyDataStore:SetAsync(playerUserId, MoneyValue)
end)
end
end)
local DataStoreService = game:GetService("DataStoreService")
local moneyDataStore = DataStoreService:GetDataStore("Money")
game:GetService("Players").PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder", player)
leaderstats.Name = "leaderstats"
local Money = Instance.new("IntValue", leaderstats)
Money.Name = "money"
local playerUserId = "player_"..player.UserId
local MoneyData
MoneyData = moneyDataStore:GetAsync(playerUserId)
if MoneyData then
Money.Value = MoneyData
else
Money.Value = 100 -- set this to your starting amount
end
end)
game:GetService("Players").PlayerRemoving:Connect(function(player)
local playerUserId = "player_" .. player.UserId
moneyDataStore:SetAsync(playerUserId, player.leaderstats.money.Value)
end)
Hello! you could also try another method where everytime player receives money it will update their money to the datastore. This MIGHT make the datastore lose perfomance, but is also a good method.
Also, the variable DataStoreService is NOT listed. This will reset players data. Feel free to criticize the idea…
Make a Script (ServerScript), and then parent it to “ServerScriptService”. Feel free to change the script’s name!
Heres an example:
local STARTER_MONEY = 10 -- Set the "10" to any number you wan't.
local get_data = function(userid)
local suc = pcall(function() game:GetService("DataStoreService"):GetDataStore("Money"):GetAsync(userid) end)
if suc then
return game:GetService("DataStoreService"):GetDataStore("Money"):GetAsync(userid)
else
game:GetService("DataStoreService"):GetDataStore("Money"):SetAsync(userid, STARTER_MONEY)
return STARTER_MONEY
end
end
game.Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder", player)
leaderstats.Name = "leaderstats"
local money = Instance.new("NumberValue", leaderstats)
money.Value = get_data(player.UserId) -- Do not worry; this will automically get player's money.
money.Changed:Connect(function(v)
task.wait(0.5) -- Keep datastores not losing perfomance.
game:GetService("DataStoreService"):GetDataStore("Money"):SetAsync(player.UserId, v)
end)
end)
No sorry I do not get any errors. I try this by editing the value from the explorer while in game and editing it to 100, I stop the game and rejoin. (just saying if thats not how you test it)