DataStores Not working?

I’m trying to make a datastore, pretty self explainatory

Whenever i load up the game, No errors. I update the value but when i leave it doesn’t give me any output message, Nor an error. It just doesn’t save.

local DataStore = game:GetService("DataStoreService")
local theStore = DataStore:GetDataStore("Im Going To Buy Some Cheese")

game.Players.PlayerAdded:Connect(function(player)
	local VelueHammer = Instance.new("IntValue", player)
	VelueHammer.Name = "ValueHammer"

	local clickDetector = game.Workspace.Spawn.Spawn["Meshes/stick"].ClickDetector
	clickDetector.MouseClick:Connect(function()
		VelueHammer.Value = VelueHammer.Value + 1

	local clickDetector = game.Workspace.ClickaPArt.ClickDetector

	local playerUserId = "Player_"..player.UserId
	print(playerUserId)

	-- Loading?

	local data
	local success = pcall(function()
		data = theStore:GetAsync(playerUserId)
	end)

	if success then
			print("our data is alive")
		else
			print("The data literally died")
			warn(error)
		VelueHammer.Value = data
	end

	game.Players.PlayerRemoving:Connect(function(player)
			local data = {
				VelueHammer = VelueHammer.Value
			}
		local playerUserId = "Player_"..player.UserId
		theStore:SetAsync(playerUserId,data.VelueHammer)
			
		end)
	end)
end)
1 Like

good luck, mark it as solved if it helped you

1 Like

Hey!
You dont want to save the datastore every time a player clicks something, that will flood the datastore capabilities.

You wanna save the datastore when the player leaves game.

I made some adjustments in your scripts.
Pulling out the PlayerRemoved event from the PlayerAdded event.

Reading the datastore when player joins, if succesful reading, give actions/connections to player.

When player leaving, reading the value inside player to save it (personally I prefer to keep player’s data on a server table and not putting values inside their player/character instances)

local DataStore = game:GetService("DataStoreService")
local theStore = DataStore:GetDataStore("SomeDataStore_01") -- your datastore name

-- When player joins
game.Players.PlayerAdded:Connect(function(player)
	-- Loading Data from player
	local success, data = pcall(function()
		return theStore:GetAsync("Player_"..player.UserId)
	end)

	if success then
		-- Place values in player once the datastore reading worked
		print("our data is alive")
		
		local VelueHammer = Instance.new("IntValue", player)
		VelueHammer.Name = "ValueHammer"
		
		if data then
			print("player in datastore found")
			VelueHammer.Value = data
		else
			print("no player in datastore, should be new player")
			VelueHammer.Value = 0
		end
		
		-- Connecting this only if datastore try was succesful
		local clickDetector = game.Workspace.Spawn.Spawn["Meshes/stick"].ClickDetector
		clickDetector.MouseClick:Connect(function()
			VelueHammer.Value = VelueHammer.Value + 1
			local clickDetector = game.Workspace.ClickaPArt.ClickDetector
			local playerUserId = "Player_"..player.UserId
			print(playerUserId)
		end)
	else
		-- datastore reading failed, retry with a loop or handle the error, do not let player to play without a proper datastore reading
		print("The data literally died, roblox issues, retry on a loop")
	end
end)

-- When player leaves
game.Players.PlayerRemoving:Connect(function(player)
	-- Save datastore only if the player has the data
	if player:FindFirstChild("ValueHammer") then
		theStore:SetAsync("Player_"..player.UserId, player.ValueHammer.Value)
	end
end)
2 Likes

Whats probably happening is you dont have a BindToClose line. In your script, you have a PlayerRemoving event, but not a BindToClose event. When you stop a playtest in studio, it shuts down the server, which is where the BTC comes in. Most of the time, saving issues are due to a lack of a btc, and its always good to have one

local DataStore = game:GetService("DataStoreService")
local theStore = DataStore:GetDataStore("Im Going To Buy Some Cheese")

game.BindToClose:Connect(function()
    for _, player in game.Players:GetPlayers() do 
        			local data = {
				VelueHammer = VelueHammer.Value
			}
		local playerUserId = "Player_"..player.UserId
		theStore:SetAsync(playerUserId,data.VelueHammer)
            
end)

Pretty sure this is where the issue lies. Try this out and lmk what happens

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.