Help saving data script

Hi, I just changed some things in the code and it doesn’t work properly, what’s wrong?

I don’t want a leaderstats folder or anything like that, just that the folder is called playerFolder.

Server Script Service:

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
	Gold.Name = "myGold"

	-- 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
	else
		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)

Before starting, what’s the problem here? You should tell us before we can help you with your specific problem.

Any errors? Did you enable https and API requests?

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?