Why my script for DataStore doesn't work?

cash.Value = dataStores.Cash:GetAsync(tostring(player.UserId)) or 0
--It first gets the data store value.
--If it's nil, then it sets to 0.

No you shouldn’t. Because it checks if the data store has any data, if it doesn’t it sets to “0”, kind of why i put or when setting values.

Yes, I have script to change Cash Value:

local plr = game.Players.LocalPlayer

TextButton.MouseButton1Click:Connect(function()
	
	plr:WaitForChild("leaderstats").Cash.Value += 19	
end)

Idk why it’s doesn’t work, for me.

:GetAsync() or :SetAsync() can sometimes throw an error, it’s always a good practice to use pcall()

1 Like

This is in a local script. That won’t work.

where I have to put this part of code?

Yes but if that throws an error, then it’ll set it to 0.

iIt either tries :GetAsync() or 0, for the value.

Just copy and paste this script. It’s organised for you:

local DSS = game:GetService("DataStoreService")
local dataStores = {
    Cash = DSS:GetDataStore("CashData")
}

game:BindToClose(function()
    task.wait(5)
end)

game.Players.PlayerAdded:Connect(function(player)
    
    local leaderstats = Instance.new("Folder", player)
    leaderstats.Name = "leaderstats"
    
    local cash = Instance.new("IntValue", leaderstats)
    cash.Name = "Cash"
    cash.Value = dataStores.Cash:GetAsync(tostring(player.UserId)) or 0
    
end)

game.Players.PlayerRemoving:Connect(function(player)
    if player:FindFirstChild("leaderstats") then
        dataStores.Cash:SetAsync(tostring(player.UserId), player.leaderstats.Cash.Value)
    end
end)

I put this in the script in the ServerScriptService

If it throws an error, it throws an error and the code stops there.
Saving Data | Documentation - Roblox Creator Hub.

No, my output is clear. Just don’t save the data

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

do you mean Put into the ServerScriptService?

Im sorry, yes I mean the serverscriptstorage

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.

The code doesn’t load the data

Yes, I know but still doesn’t work :confused: 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 ._.

Ah, let me take a look again. And try to debug it.

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)

EDIT: I ran it using gpt 4.0

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.

I didn’t fully reply using it but I apologize it didn’t work I’m only trying to help you.