Datastore service dosen't save my string value

It saves every other value but it dosent save the string and says “string expected, got nil”

local DS = game:GetService("DataStoreService"):GetDataStore("Clicks")

game.Players.PlayerAdded:Connect(function(Plr)
	local leaderstats = Instance.new("Folder",Plr)
	leaderstats.Name = "leaderstats"
	local clicks = Instance.new("IntValue",leaderstats)
	clicks.Name = "Clicks"
	local coins = Instance.new("IntValue",leaderstats)
	coins.Name = "Coins"
	local aura = Instance.new("StringValue",leaderstats)
	aura.Name = "Aura"
	
	local data = DS:GetAsync(Plr.UserId)

	if data ~= nil then
		clicks.Value = data[1]
		coins.Value = data[2]
		aura.Value = data[3]
	else
		clicks.Value = 0
		coins.Value = 0
		aura.Name = "White"
	end
	
end)

game.Players.PlayerRemoving:Connect(function(Plr)
	local save = {}
	table.insert(save,Plr.leaderstats.Clicks.Value)
	table.insert(save,Plr.leaderstats.Coins.Value)
	table.insert(save,Plr.leaderstats.Aura.Value)
	DS:SetAsync(Plr.UserId,save)
end)

You’re setting the name of the aura to White, when you should be changing the aura value.

	if data ~= nil then
		clicks.Value = data[1]
		coins.Value = data[2]
		aura.Value = data[3]
	else
		clicks.Value = 0
		coins.Value = 0
		aura.Name = "White" -- here
	end

Ops i just realised that let me try

it says the same thing :((
i changed the dot name to dot value

shouldn’t your datastore be in a pcall(), at least all the times i’ve done with datastores i’ve had the datastore in a pcall. I don’t know much about data stores but i think this could possibly be the problem. don’t quote me on that

for the getting the info:

local success,data = pcall(function()
return DS:GetAsync(Plr.UserId)
end)

for saving:

local success = pcall(function()
DS:SetAsync(Plr.UserId,save)
end)

im gonna try it rn, i hope it works

it does not work sadly :(( i think its a datastore problem

L, my data store knowledge is about as much as this
https://developer.roblox.com/en-us/articles/Data-store

yup sadly i am a noob to datastores too

can i add you on roblox :smile: ? you seem nice

i try to be nice, but i like to keep friends list with friends, but i wish you luck on the data store problem

I fixed it! Yay, i just added tostring when i save the value

1 Like
local Players = game:GetService("Players")
local DataStores = game:GetService("DataStoreService")
local DataStore = DataStores:GetDataStore("DataStore")

local ProtectedCall = pcall

Players.PlayerAdded:Connect(function(Player)
	local Leaderstats = Instance.new("Folder")
	Leaderstats.Name = "leaderstats"
	Leaderstats.Parent = Player
	
	local Clicks = Instance.new("IntValue")
	Clicks.Name = "Clicks"
	Clicks.Parent = Leaderstats
	
	local Coins = Instance.new("IntValue")
	Coins.Name = "Coins"
	Coins.Parent = Leaderstats
	
	local Aura = Instance.new("StringValue")
	Aura.Name = "Aura"
	Aura.Parent = Leaderstats

	local Success, Result = ProtectedCall(function()
		return DataStore:GetAsync("Stats_"..Player.UserId)
	end)
	
	if Success then
		if Result then
			if type(Result) == "table" then
				Clicks.Value = Result[1] or 0
				Coins.Value = Result[2] or 0
				Aura.Value = Result[3] or ""
			end
		end
	end
end)

Players.PlayerRemoving:Connect(function(Player)
	local Success, Result = ProtectedCall(function()
		return DataStore:SetAsync("Stats_"..Player.UserId, {Player.leaderstats.Clicks.Value, Player.leaderstats.Coins.Value, Player.leaderstats.Aura.Value})
	end)
	
	if Success then
		print(Result)
	else
		warn(Result)
	end
end)

game:BindToClose(function()
	for _, Player in ipairs(Players:GetPlayers()) do
		local Success, Result = ProtectedCall(function()
			return DataStore:SetAsync("Stats_"..Player.UserId, {Player.leaderstats.Clicks.Value, Player.leaderstats.Coins.Value, Player.leaderstats.Aura.Value})
		end)

		if Success then
			print(Result)
		else
			warn(Result)
		end
	end
end)

Make sure no other script(s) is/are changing the name of any of the leaderstats as one of the previous replies pointed out.

pcall() isn’t required but it’s recommended because if the DataStore request fails then an error is thrown/raised, by wrapping the DataStore request inside a pcall (protected call), if an error is thrown/raised it is handled by the pcall, if the request was not wrapped inside a pcall when it failed then the execution of the entire script would break (abruptly stop) as the thrown/raised error wouldn’t be handled.

This isn’t necessary, when a “StringValue” instance is created its “Value” property points to nil unlike other similar instances such as “NumberValue”/“IntValue” which have their “Value” property assigned a value of 0 by default. Due to this the DataStore was saving a nil value (what represents a lack of a value in Lua) for the “StringValue” instance.

To remedy this you can just set some default value for the “Value” property of the “StringValue” instance, typically I go with an empty string value "", it’s still a string value but it’s a good way of representing nothing (like 0 for numbers/integers). By doing this, when the data is saved to the DataStore this default empty string value will be stored instead of a nil value.