Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Datastore won't load data?

Code below won't load data, no errors, I have no idea what's going on.

local dataStoreService = game:GetService("DataStoreService")
local cubeDataStore = dataStoreService:GetDataStore("Cubes")

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

	local cubesmade = Instance.new("IntValue",leaderstats)
	cubesmade.Name = "Cubes"
	cubesmade.Value = 0

	local playerUserId = "player_"..player.UserId
	local cubeData
	local success, errormessage = pcall(function()
		cubeData = cubeDataStore:GetAsync(playerUserId)
	end)

	if success then
		cubesmade.Value = cubeData
	end
end)

game.Players.PlayerRemoving:Connect(function(player)
	local playerUserId = "player_"..player.UserId
	local cubeValue = player.leaderstats.Cubes.Value
	local success, errormessage = pcall(function()
		cubeDataStore:SetAsync(playerUserId, cubeValue)
	end)
end)

game:BindToClose(function()
	for _, player in pairs(game.Players:GetPlayers()) do
		local playerUserId = "player_"..player.UserId
		local cubeValue = player.leaderstats.Cubes.Value
		local success, errormessage = pcall(function()
		end)
		cubeDataStore:SetAsync(playerUserId, cubeValue)
	end
end)

1 comment

0

Looks fine, after you checking if success is true add an else and then print the error message because your code wont print one unless you tell it to from the pcall.

1 answer

Try this

local DSS = game:GetService("DataStoreService")

local DataStore = DSS:GetDataStore("CubicData")







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

	local leaderstats = Instance.new("Folder")

	leaderstats.Parent = player



	local Cubes = Instance.new("IntValue")

	Cubes.Name = "Cubes"

	Cubes.Parent = leaderstats



	local data

	local success, errorMessage = pcall(function()

		data = DataStore:GetAsync(player.UserId)

	end)

	if success and data ~= nil then

		print("Data successfully loaded!")

		Cubes.Value = data.Cubes

	else

		warn(errorMessage)

	end

end)



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

	local leaderstats = player.leaderstats

	local data = {

		Cubes = leaderstats.Cubes.Value; 

	}

	local success, errorMessage = pcall(function()

		DataStore:SetAsync(player.UserId,data)

	end)

	if success then

		print("Data successfully saved!")

	else

		warn(errorMessage)

	end

end)

Hope this helps!!

2 comments

0

It works now, but all it does is print nil and doesn't start the values.

0

? It works fine to me without any errors?

This discussion is preserved as it was. New answers, comments, and votes are closed.