Put this in the ServerStorageScript.
Run the game.
To change the value of the cash, go to TEST > Current: Client. Now, in the explorer window, go to Players > YOUR_PLAYER_NAME > leaderstats > Cash and change it from there
You have to go to the server side before changing the value
The script you provided seems to be correct, but I’ve made a small adjustment to ensure proper handling of errors. Additionally, I’ve renamed the variable error to errorMsg to avoid conflicts with the built-in error function. Here’s the modified version of your script:
local Players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")
local CASH_DATA_STORE_NAME = "PlayerCashData"
Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local Cash = Instance.new("NumberValue")
Cash.Name = "Cash"
Cash.Value = 0
Cash.Parent = leaderstats
end)
local function savePlayerData(player)
local leaderstats = player:FindFirstChild("leaderstats")
if leaderstats then
local cashValue = leaderstats:FindFirstChild("Cash")
if cashValue then
local dataStore = DataStoreService:GetDataStore(CASH_DATA_STORE_NAME)
local key = "Player_" .. player.UserId
local success, errorMsg = pcall(function()
dataStore:SetAsync(key, cashValue.Value)
end)
if not success then
warn("Error saving player data for " .. player.Name .. ": " .. errorMsg)
end
end
end
end
Players.PlayerRemoving:Connect(function(player)
savePlayerData(player)
end)
This script should work correctly to create and save player data to the DataStore. Make sure you have the DataStore named “PlayerCashData” created in the DataStoreService. Also, ensure that the script is placed in a location where it will run properly, such as a Server Script in Roblox Studio.
Yes, I know but still doesn’t work Maybe thats error in Roblox Studio cuz I tried so many tutorial, I tried To read documentation. I asked f###ng chat GPT but it still doesn’t work ._.
This is what I’m getting from chatgpt from that issue:
local Players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")
local CASH_DATA_STORE_NAME = "PlayerCashData"
Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local Cash = Instance.new("NumberValue")
Cash.Name = "Cash"
Cash.Value = 0
Cash.Parent = leaderstats
-- Load player data here
local dataStore = DataStoreService:GetDataStore(CASH_DATA_STORE_NAME)
local key = "Player_" .. player.UserId
local success, value = pcall(function()
return dataStore:GetAsync(key)
end)
if success then
if value ~= nil then
Cash.Value = value
end
else
warn("Error loading player data for " .. player.Name .. ": " .. value)
end
end)
local function savePlayerData(player)
local leaderstats = player:FindFirstChild("leaderstats")
if leaderstats then
local cashValue = leaderstats:FindFirstChild("Cash")
if cashValue then
local dataStore = DataStoreService:GetDataStore(CASH_DATA_STORE_NAME)
local key = "Player_" .. player.UserId
local success, errorMsg = pcall(function()
dataStore:SetAsync(key, cashValue.Value)
end)
if not success then
warn("Error saving player data for " .. player.Name .. ": " .. errorMsg)
end
end
end
end
Players.PlayerRemoving:Connect(function(player)
savePlayerData(player)
end)
Bro, never use Chat GPT as something harder to “print(“hello world!”)” cha GPT its only beta model of AI and it can’t produce you high Quality Code. It will just make alot of junk function, also Roblox Studio DevForum have right to doesn’t use chat GPT to reply.