I cannot load StringValue with DataStoreService

I got some issues with my DataStore script, the OutPut print an error when I try to load StringValue.

`local DataStore = game:GetService(“DataStoreService”):GetDataStore(“DataName”)
game.Players.PlayerAdded:connect(function(player)

local key = “key-”…player.userId

local PlayerData = Instance.new(“Folder”,player)
Upgradesfolder.Name = “PlayerData”

local PlayerLocation = Instance.new(“StringValue”,PlayerData)
PlayerLocation.Name = “PlayerLocation”
PlayerLocation.Value = “None”

local save = DataStore:GetAsync(key)
if save then
PlayerLocation.Value = save[1] --where the error is triggered
else
local loadPlayerLocation = {PlayerLocation.Value}
DataStore:SetAsync(key,loadPlayerLocation)
end`

The error says : " [Workspace.PlayerDataCreationLoad:307: bad argument #3 (string expected, got boolean)]"

I know that the error come from the fact that it is a Stringvalue and not an Intvalue but I don’t know how to fix this. Can you please help me?

Thanks!

1 Like

The error states clearly that the 3rd argument should be a string but its not getting a string its getting a boolean value (true/false).

Print the value of “save[1]” what is it?

local DataStore = game:GetService("DataStoreService"):GetDataStore("DataName")

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

	local key = "key-"..player.userId

	local PlayerData = Instance.new("Folder", player)
	Upgradesfolder.Name = "PlayerData"
	
	local PlayerLocation = Instance.new("StringValue",PlayerData)
	PlayerLocation.Name = "PlayerLocation"
	PlayerLocation.Value = "None"
	
	local save = DataStore:GetAsync(key)
	if save then
		PlayerLocation.Value = save[1] --where the error is triggered
	else
		local loadPlayerLocation = {PlayerLocation.Value}
		DataStore:SetAsync(key,loadPlayerLocation)
	end
end)

I reformatted your script here so we could read it more easily. I fixed it here or there too so you might want to try it first.

It says “False” (note that the script I put up there was only a part of the script and that there is a lot of other value including BoolValue and IntValue).

Should I redefine save[1] in the script?

ok so save 1 is false right. thats boolean. Save[1] value is created when you store the data so go to that code and see why save[1] is false and not a string or w/e it should be.

You might be having a problem here too:

DataStore:GetAsync(key)

Try wrapping it in a pcall to check for success then verify the data you got and then use it.

local success, err = pcall(function()
    save = DataStore:GetAsync(key)
end)

if(success)then
    PlayerLocation.Value = save[1]
else
    local loadPlayerLocation = {PlayerLocation.Value}
    DataStore:SetAsync(key,loadPlayerLocation)
end

I think that save[1] correspond to the data of the BoolValue (wich is false) I created first in the script. I modified the script and it looks like this:

BoolValue1.Value = save[1]
BoolValue2.Value = save[2]
BoolValue3.Value = save[3]
BoolValue4.Value = save[4]
BoolValue5.Value = save[5]
[…]
StringValue40.Value = save[40]

But now, it says that save[40] is nil.

idk bud you lost me. You might have to put up more of the code. Make sure to wrap code with “</>” button.

<local DataStore = game:GetService(“DataStoreService”):GetDataStore(“DataName”)
game.Players.PlayerAdded:connect(function(player)
local key = “key-”…player.userId

–Folder and Value creation

local Valuefolder = Instance.new(“Folder”,player)
Valuefolder.Name = “Valuefolder”

local BoolValue1 = Instance.new(“BoolValue”,Valuefolder)
BoolValue1.Name = “Something1”
BoolValue1.Value = false
local BoolValue2 = Instance.new(“BoolValue”,Upgradesfolder)
BoolValue2.Name = “Something2”
BoolValue2.Value = false

[…]

local StringValue = Instance.new(“StringValue”,Valuefolder)
StringValue.Name = “Something40”
StringValue.Value = false

local save = DataStore:GetAsync(key)
if save then
Boolvalue1.Value = save[1]
Boolvalue2.Value = save[2]
[…]
StringValue.Value = save[40] --Where the error is triggered - It says that save[40] is nil.
else
local LoadBoolValue1 = {BoolValue1.Value}
DataStore:SetAsync(key,LoadBoolValue1)
local LoadBoolValue2 = {BoolValue2.Value}
DataStore:SetAsync(key,LoadBoolValue2)
[…]
local LoadStringValue = {StringValue.Value}
DataStore:SetAsync(key,LoadStringValue)
end>

This is an abstract version of my DataStore Script (the new one).

if save then
	Boolvalue1.Value = save[1]
	Boolvalue2.Value = save[2]

The V in value should be capitalized to match:

local BoolValue1 = Instance.new("BoolValue",Valuefolder)
BoolValue1.Name = "Something1"
BoolValue1.Value = false

Here the same key is used to store two different values, so the 2nd value is crushing the first.

	local LoadBoolValue1 = {BoolValue1.Value}
	DataStore:SetAsync(key,LoadBoolValue1)
	
	local LoadBoolValue2 = {BoolValue2.Value}
	DataStore:SetAsync(key,LoadBoolValue2)

Also not sure why you store the value as a table/array.

local key = "key-"..player.UserId

I made a few adjustments to the script you posted. I think my changes represent what your trying to accomplish.

local PlayerPositions = game:GetService("DataStoreService"):GetDataStore("PlayerPositions")

game.Players.PlayerAdded:connect(function(player)
	
	--Create the key to use for this player
	local key = "key-"..player.userId
	
	--Folder and Value creation
	local ValueFolder = Instance.new("Folder", player)
	ValueFolder.Name = "Valuefolder"
	
	local BoolValue1 = Instance.new("BoolValue", ValueFolder)
	BoolValue1.Name = "Something1"
	BoolValue1.Value = false
	
	local BoolValue2 = Instance.new("BoolValue", UpgradesFolder)
	BoolValue2.Name = "Something2"
	BoolValue2.Value = false

	--[…]

	local StringValue = Instance.new("StringValue", ValueFolder)
	StringValue.Name = "Something40"
	StringValue.Value = false
	
	
	--Try to retrieve stored data
	local saveData = nil
	local success, msg = pcall(function()
		saveData = PlayerPositions:GetAsync(key)
	end)
	
	--verfy 
	if(saveData ~= nil)then
		--Grab the data
		BoolValue1.Value = saveData[1]
		BoolValue2.Value = saveData[2]
		--etc...
		StringValue.Value = saveData[40]
	else
		--Store default data
		local newData = {BoolValue1.Value, BoolValue2.Value, StringValue.Value}
		
		local success, msg = pcall(function()
			PlayerPositions:SetAsync(key, newData)
		end)
		
		if(success)then
				print("Player Position Data Was Saved")
		else
			print("Could not save player position data: ".. msg)
		end
	end
end)

I rewrote my script according to yours. I wrote a new DataStoreName. When I launch a first time, the OutPut says that my Data is save, then I make some modifications in my player’s data and go to a save station which save my data when I walk on a part. I quit and when I launch the game again, the Data did not save and the Output triggered nothings (not even the message that says i am connected)

Thanks for the rewrote script anyways!