Can anyone tell me why this doesn't work?

Hey Roblox devs. I have a datastore system in my game, and it doesn’t seem to work…I’ve tried looking at tutorials, reading things about datastores, and I tried to apply everything I know into this but I tested it and it doesn’t save any data. Not sure if it’s a problem with how it saves the data, or how it loads the data. Here is my script:

local DataStoreService = game:GetService("DataStoreService")
local Players = game:GetService("Players")
local PlayerData = DataStoreService:GetDataStore("PlayerData")
local MultiplayerData = DataStoreService:GetDataStore("MultiplayerData")


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

	local Coins = Instance.new("NumberValue", leaderstats)
	Coins.Name = "Coins"
	Coins.Value = 25
	local XP = Instance.new("NumberValue", leaderstats)
	XP.Name = "XP"
	local Level = Instance.new("NumberValue", player)
	Level.Name = "Level"
	local Rank = Instance.new("NumberValue", player)
	Rank.Name = "Rank"

	local GunsUnlocked = Instance.new("Folder", player)
	GunsUnlocked.Name = "GunsUnlocked"
	--add each unlockable gun as a bool value

	local GunLevels = Instance.new("Folder", player)
	GunLevels.Name = "GunLevels"
	local BlackoutLevel = Instance.new("NumberValue", GunLevels)
	BlackoutLevel.Name = "BlackoutLevel"
	local MP7Level = Instance.new("NumberValue", GunLevels)
	MP7Level.Name = "MP7Level"

	local campaignLevel = Instance.new("NumberValue", player)
	campaignLevel.Name = "campaignLevel"

	local CurrentObjective = Instance.new("NumberValue", player)
	CurrentObjective.Name = "CurrentObjective"

	local playerID = player.UserId
	local data
	local success, errormessage = pcall(function()
		data = PlayerData:GetAsync(playerID)
	end)

	if success then
		Coins.Value = data
		XP.Value = data
		Level.Value = data
		Rank.Value = data

		BlackoutLevel.Value = data

		campaignLevel.Value = data

		CurrentObjective.Value = data

		print("loaded data")
	end
end)

Players.PlayerRemoving:Connect(function(player)
	local playerID = player.UserId

	local data = {
		Coins = player.leaderstats.Coins.Value;
		XP = player.leaderstats.XP.Value;
		Level = player.Level.Value;
		Rank = player.Rank.Value;

		BlackoutLevel = player.GunLevels.BlackoutLevel.Value;
		MP7Level = player.GunLevels.MP7Level.Value;

		campaignLevel = player.campaignLevel.Value;

		CurrentObjective = player.CurrentObjective.Value;
	}

	local success, errormessage = pcall(function()
		PlayerData:SetAsync(playerID, data)
	end)

	if success then
		print("saved data")

	else
		print("data didn't save")
		warn(errormessage)
	end
end)

I honestly have no clue how to fix my errors. I’m completely lost. The DevForum is my last resort, so I would appreciate it if someone could let me know what it wrong with my script.

1 Like

Well, didja get any errors and such?

		Coins.Value = data
		XP.Value = data
		Level.Value = data
		Rank.Value = data

		BlackoutLevel.Value = data

		campaignLevel.Value = data

		CurrentObjective.Value = data

		print("loaded data")

i believe data is stored in a table
meaning you would have to do something like

    Coins.Value = data.Coins
    XP.value = data.XP
    Level.Value = data.Level
    Rank.Value = data.Rank

    BlackoutLevel.Value = data.BlackoutLevel

    CurrentObjective.Value = data.CurrentObjective

    print("loaded data")

as I was tesing in studio I got error 104: Cannot store Dictionary in data store. Data stores can only accept valid UTF-8 characters.

I doubt that’s why it’s not working though, it just recently popped up I’m trying to find a solution for it right now. But, I still could be wrong.

try using Intvalues instead of numberValues

I don’t see how that would fix the problem

i believe number values uses double-precision floating point number which might be causing the error

It’s still giving me the error. Now I’m starting to think this is why it’s not saving. Currently, I’m trying to use a module script to store the data there but I don’t understand how I would be able to store the data there.

why would you want to store data with a module

I saw this so I thought I should try it.

is your code inside the module?

The code that saves your data is in a server script, what I’m trying to do is instead of storing the data on the serverscript, I could use a module script for it. I’m just trying out ideas to fix the error. I learned that the problem is saving the player data, so if you go back and check my script the “save data” area is where Player.playerRemoved is. The data I’m storing in the module script is the

local data = {
      *data stuff
}

I’m not completely sure but wouldn’t the data variable inside the Player Added function be equal to nothing because the data value is set outside in the Player Removing function, shouldn’t you have the data variable outside both functions at the start and then reference the table in the functions with checks to make sure they don’t overwrite each other.

Try running this in a live game (not Studio).