Datastore doesn't save

When I make a script for datastore, It only works once, then it doesnt save anymore.

I have tried a couple of solutions like: Making seperate functions for saving and alot more, but I keep running into the same problem.

My code:

game.Players.PlayerRemoving:Connect(function(client)
	SaveData(client)
end)

function SaveData(client)
		local ClientId = client.UserId
		local Data1 = script.HairId.Value
		local Data2 = script.SkinColor.Value

		local success, err = pcall(function()
			HairId:SetAsync(ClientId,Data1)
		end)
		local success2, errorMessage = pcall(function()
			ColorId:SetAsync(ClientId,Data2)
		end)
		print(success)
		if success then
			print("Success")
		end
		print(success2)
end

Does one of you know the problem? Thanks!

You need to create a datastore

If it is saving once, it seems like it’s working and maybe the data is getting overwritten somewhere, like when the player joins?

I am assuming HairId and SkinColor are string values parented to the script itself?
Would it be possible to set them up as string values that are parented to the player’s leaderstats so that when another player joins, they don’t get overwritten?

Sorry I couldn’t be of more help, but I don’t see any issues with the code itself.

I have done that, I forgot to put that in the codeblock.

After I make the function, the code saves the datastore once.
Then the function just stop responding,

		print(success)
		if success then
			print("Success")
		end

Doesnt even print anymore.

Maybe you are saving the datastore too quickly many times? DataStore canT handle many request in a few seconds

I would suggest to store both data too. Its not exactly related with ur problem, but could be useful

local DSS = game:GetService("DataStoreService")
local HairValues = DSS:GetDataStore("PlayerThings")

function SaveData(client)
	local ClientId = client.UserId
	local Data1 = script.HairId.Value
	local Data2 = script.SkinColor.Value

	local success, err = pcall(function()
		HairValues:SetAsync(ClientId, {Data1, Data2})
	end)

	print(success)
	if success then
		print("Success")
	end
end

game.Players.PlayerRemoving:Connect(function(client)
	SaveData(client)
end)

Instead of having two datastores for the character’s appearance it would be better to save it into a table then have one.

local DataStoreService = game:GetService(DataStoreService
local appearanceDataStore = DataStoreService:GetDataStore(“appearanceDataStore”)

game.Players.PlayerRemoving:Connect(function(client)
SaveData(client)
end)

function SaveData(client)
local ClientId = client.UserId
local Data1 = script.HairId.Value
local Data2 = script.SkinColor.Value

	local success, err = pcall(function()
		HairId:SetAsync(ClientId,Data1)
	end)
	local success2, errorMessage = pcall(function()
		ColorId:SetAsync(ClientId,Data2)
	end)
	print(success)
	if success then
		print("Success")
	end
	print(success2)

end

Instead of having two datastores for the character’s appearance it would be better to save it into a table then have one.

local DataStoreService = game:GetService(DataStoreService
local AppearanceDataStore = DataStoreService:GetDataStore(“AppearanceDataStore”)

game.Players.PlayerRemoving:Connect(function(client)
	SaveData(client)
end)

function SaveData(client)
local Data = {
HairId = script.HairId.Value,
SkinColor = script.SkinColor.Value}

		local success, err = pcall(function()
			AppearanceDataStore:SetAsync(ClientId, Data)
		end)

		print(success)
		if success then
			print("Success")
		end
end

This works for storing, but how could I get the 2 values back out of the table?

Continue of my example, get the 2 tables/values like this:

local success, gotTable = pcall(function()
	return HairValues:GetAsync(UserID)
	end)

print(gotTable[1])
print(gotTable[2])

Sorry for the late reply I had to remind myself how to do this.

function loadData(player)
local data
	local success, errormessage = pcall(function()
		data = appearanceDataStore:GetAsync(player.UserId)
	end)
local HairId = data.HairId
local SkinColor = data.SkinColor
end

game.Players.PlayerAdded:Connect(loadData)

Running into the same problem, it saves once and then doesnt save anymore.
So after the first time saving the table.
It doesnt return any errors, but also doesnt return any prints with the success value.

Datastores don’t work the best in studio maybe try it in the actual game?

Problem fixed itself after using.


local function

game.Players.PlayerRemoving:connect()

Instead of

game.Players.PlayerRemoving:Connect(function()

end)

Only loading always returns a empty string.

1.Use keys to store plr data
2.Never store .Value in a variable, for example.

Example
local a = script.Parent.Value
print(a) -- prints nothing.

local b = script.Parent
print(b.Value) -- prints the value.
Fixed Code
game.Players.PlayerRemoving:Connect(function(client)
	SaveData(client)
end)

function SaveData(client)
		local ClientId = "Key-"..client.UserId
		local Data1 = script.HairId
		local Data2 = script.SkinColor

		local success, err = pcall(function()
			HairId:SetAsync(ClientId,Data1.Value)
            ColorId:SetAsync(ClientId,Data2.Value)
		end)
		print(success)
		if success then
			print("Success")
		end
end

Thanks, that also fixed my problem.

1 Like