Data Store dont save nor studio or game!

Hello there this is my first topic here, so I hope It goes well :sweat_smile:

My issue is that I get a datastore from youtube and dev hub and it works with ppl I get from but with me no!

I tried more than 10 scripts!
But any of the scripts didn’t work with me, and it works with people I get from

local DataStoreService = game:GetService("DataStoreService")

local playerData = DataStoreService:GetDataStore("PlayerData")


local function onPlayerJoin(player)  -- Runs when players join

	local leaderstats = Instance.new("Folder")  --Sets up leaderstats folder

	leaderstats.Name = "leaderstats"

	leaderstats.Parent = player

	local gold = Instance.new("IntValue") --Sets up value for leaderstats

	gold.Name = "Gold"

	gold.Parent = leaderstats

	local playerUserId = "Player_" .. player.UserId  --Gets player ID

	local data = playerData:GetAsync(playerUserId)  --Checks if player has stored data

	if data then

		-- Data exists for this player
		print("Data loaded")
		gold.Value = data

	else

		-- Data store is working, but no current data for this player
		print(player.Name..(" Has no Data!"))
		gold.Value = 0

	end

end

local function onPlayerExit(player)  --Runs when players exit
	local success, err = pcall(function()

		local playerUserId = "Player_" .. player.UserId
		
		print('Data Saved!')
		playerData:SetAsync(playerUserId, player.leaderstats.Gold.Value) --Saves player data

	end)

	if not success then
		
		print("Data Could Not Save!")
		warn('Could not save data!')
	end
end

game.Players.PlayerAdded:Connect(onPlayerJoin)

game.Players.PlayerRemoving:Connect(onPlayerExit)

You should add more pcalls to your script. Try this:

local DataStoreService = game:GetService("DataStoreService")
local playerData = DataStoreService:GetDataStore("PlayerData")

local function onPlayerJoin(player)  -- Runs when players join

	local leaderstats = Instance.new("Folder")  --Sets up leaderstats folder
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player

	local gold = Instance.new("IntValue") --Sets up value for leaderstats
	gold.Name = "Gold"
	gold.Parent = leaderstats

	local playerUserId = "Player_" .. player.UserId  --Gets player ID

	local data

	local success, errmessage = pcall(function()
		data = playerData:GetAsync(playerUserId)  --Checks if player has stored data
	end)

	if success then -- The datastore works
		if data then -- Data exists for this player
			print("Data loaded")
			gold.Value = data
		else -- The player has no data
			print(player.Name..(" has no Data!"))
			gold.Value = 0
		end
	else -- The datastore failed
		print(errmessage)
	end
end

local function onPlayerExit(player)  --Runs when players exit
	local playerUserId = "Player_" .. player.UserId
	
	local success, err = pcall(function()
		playerData:SetAsync(playerUserId, player.leaderstats.Gold.Value) --Saves player data
	end)

	if not success then
		print("Data Could Not Save!")
		warn('Could not save data!')
	else
		print('Data Saved!')
	end
end

game.Players.PlayerAdded:Connect(onPlayerJoin)
game.Players.PlayerRemoving:Connect(onPlayerExit)
1 Like

It is not saved since, in a normal test, you are only in studio and surely the server shuts down before it is saved, you must use BindToClose, try this.

local DataStoreService = game:GetService("DataStoreService")
local playerData = DataStoreService:GetDataStore("PlayerData")

game:GetService("Players").PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player

	local gold = Instance.new("IntValue")
	gold.Name = "Gold"
	gold.Parent = leaderstats

	local success, err = pcall(function()
		gold.Value = playerData:GetAsync("Player_"..player.UserId) or 0
	end)
	if not success then  print("Datastore error - GetAsync:", err)  end		
end)

function Save(player)
	local success, err = pcall(function()
		playerData:SetAsync("Player_"..player.UserId, player.leaderstats.Gold.Value)
	end)
	if not success then  print("Datastore error - SetAsync:", err)  end
end
game:GetService("Players").PlayerRemoving:Connect(Save)

game:BindToClose(function()
	for _,player in pairs(game:GetService("Players"):GetPlayers()) do
        coroutine.wrap(Save)(player) 
    end
	wait(2)
end)

the coroutine is for speed.

didn’t work too, thanks for trying to help me <3

didn’t work as well, thanks for trying to help me <3

I am really confused why it dont work, however, it prints and works well, it dont save or load

did you publish the game and enable api services?

yes, I already did
as I said i got it from youtube and searched a lot of dev hub topics about datastore

Saving doesn’t often work in studio because it doesn’t give it enough time. Saving only works when the game is published and to test it go onto the website and try it like a regular game, if that then doesn’t work you have an issue in your code.
When I’m testing saving in studio to give it enough time you can kick yourself, the command for that is:

game.Players.cando_H:Kick()

Just change ‘cando_H’ to your name

It still didn’t work, also can u tell me where to put it, so I make sure I put it in the right place

game:GetService("Players").PlayerRemoving:Connect(Save)

game:BindToClose(function()
	for _,player in pairs(game:GetService("Players"):GetPlayers()) do
        coroutine.wrap(Save)(player) 
        game.Players.["0MAR280"]:Kick()
    end
	wait(2)
end)

That isn’t quite what I mean. Have you tried using it from the website? What I was saying was testing I use the Command Bar to type a kick command in that gives it enough saving time.

i dont know what is the the problem

Is it not working in game there? Then it would be an issue with the code, I was talking about kicking while still in studio

don’t work in-game or even studio :sad:

Try instead:

game:GetService(“Players”)[“0MAR280”]:Kick()

but if it isn’t working in game then this isn’t the issue in any means

Not working as well :tired_face:


Lol, it’s because it doesn’t like your name, I can’t remember how to fix that to get past it but one option could be while previewing in explorer click “Players” and then click your player and change it’s name to something else such as “MAR” then do

game:GetService(“Players”)[“MAR”]:Kick()

It still don’t work, I appreciate your help, thanks so much for your time :pray:

Every time I face a problem, I fix it but this problem since more than 1 month, and for now, I can’t fix it
I am tired

Just still checking so you’ve tested it while not on studio right

Sorry for the late replay, yes i tested it in both

I still need help please if anybody can help here

Please if anybody can help, my game needs it for real, I don’t know what Is the problem happening with me! :tired_face: