Help saving data script

Have you tested it in the real game, datastores often don’t work well in Studio.

As @XdJackyboiiXd21 said, the playerremoving event usually doesn’t fire in studio.

The problem is that when I go to roblox player and run the below command f9, it doesn’t show me my int value and neither can it do + = 1 in the int value

print(game.Players.MatiasHarders.playerFolder.myGold.Value)

It doesn’t show my int value and I can’t increase it either

Weird. Looks like the script should work.

In the player added function, at the bottom of it can you print(data), to see if it did save it?

1 Like

it didn’t saved something is wrong in the code :frowning:

image

image

i went to roblox player and it doesn’t print anything

i went to roblox player and it doesn’t print anything


local Players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")
local Store = DataStoreService:GetDataStore("data") 


game:GetService("Players").PlayerAdded:Connect(function(Player)
	local Folder = Instance.new("Folder",Player)
	Folder.Name = "PlayerFolder"
	local Gold = Instance.new("IntValue",Folder)
	Gold.Name = "Gold" 
	local success, currentPoints = pcall(function()
		return Store:GetAsync(Player.UserId)
	end)
	if success then
		Gold.Value = currentPoints
	end

	Gold.Changed:connect(function()
		pcall(function()
			Store:SetAsync(Player.UserId, Gold.Value)
		end)
	end)
end)

Sadly PlayerRemoved sometimes doesn’t always function so you have to use BindToClose which delays the close time, I hate that. What I do is I check for changes in the value.

Could this be the problem?

script:

-- SAVEDATA
local DataStore =  game:GetService("DataStoreService"):GetDataStore("myData")
game.Players.PlayerAdded:Connect(function(player)

	local playerFolder = Instance.new("Folder", player)
	playerFolder.Name = "playerFolder"
	local Gold = Instance.new("IntValue", playerFolder) -- blah blah make your stats
	

	-- Load data

	local data
	local key = "Player_".. player.UserId
	local success, errormessage = pcall(function()
		data = DataStore:GetAsync(key)
	end)
	if success then
		Gold.Value = data
	elseif data == nil then
            Gold.Value = 0
		print("gold has been saved.")
	else
		print("error saving data, error saving data!!!")
		warn(errormessage)
	end
end)

game.Players.PlayerRemoving:Connect(function(player)
	local key = "Player_".. player.UserId

	local data = player.playerFolder.Gold.Value

	DataStore:SetAsync(key, data)
end)

You should check firstly whats in Datastore’s folder of player.
Anyways, i think the issue is below the " local Gold " you didn’t gave it a name
( Actually to save that instance )

1 Like

Still not working :frowning:

and i named it

image

I tried to change the name of it and it worked.

but i don’t actually think that’s the problem. Why not your data name ?

1 Like

Omg how!? what was wrong! :scream:

Could you share the code with me, please :frowning:

– SAVEDATA
local DataStore = game:GetService(“DataStoreService”)
local PlayerData = DataStore:GetDataStore(“playerGold”)

game.Players.PlayerAdded:Connect(function(player)

local playerFolder = Instance.new("Folder", player)
playerFolder.Name = "Data"

local Gold = Instance.new("IntValue", playerFolder) -- blah blah make your stats
Gold.Name = "Gold"
-- Load data

local data
local key = "Player_".. player.UserId
local success, errormessage = pcall(function()
    data = DataStore:GetAsync(key)
end)


if success then
    Gold.Value = data
elseif data == nil then
    Gold.Value = 0
    print("gold has been saved.")
else
    print("error saving data, error saving data!!!")
    warn(errormessage)
    end
end)

game.Players.PlayerRemoving:Connect(function(player)
local key = “Player_”… player.UserId

local data = player.playerFolder.Gold.Value

DataStore:SetAsync(key, data)

end)

1 Like

Anyways, there is a error regard the SetAsync() function.
Consider looking at this : Stop using SetAsync() to save player data

In roblox studio it makes me print “gold has been saved.” But in roblox player I get this error, I have the API activated, I don’t know what is wrong …

image

Oh I think I am right! omg … I put save to roblox and had not put publish to roblox, does this affect?

OMG!! NOW WORKS!

Sorry for the inconvenience guys, I think it was my lack of experience :frowning: I had not put publish to roblox lol

Thats why you couldn’t load " DataStores " on F9. Now try to print the Folder technically it will works.

1 Like

This works perfectly, thank you very much for your time and help, really :smiley: