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

how do i save this stringvalue in my datastore?

i'm fairly new to datastores even though i understand the basics, and my datastore would save the intvalues as it should. but when i tried to make it save a stringvalue, it says:

invalid argument #3 (string expected, got nil)

here's the code:

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

game.Players.PlayerAdded:Connect(function(player)
	local l = Instance.new("Folder")
	l.Name = "leaderstats"
	l.Parent = player
	
	local wins = Instance.new("IntValue")
	wins.Name = "Wins"
	wins.Value = 0
	wins.Parent = l
	
	local cash = Instance.new("IntValue")
	cash.Name = "Cash"
	cash.Value = 0
	cash.Parent = l
	
	local swordType = Instance.new("StringValue")
	swordType.Name = "SwordType"
	swordType.Value = "ClassicSword"
	swordType.Parent = player
	
	local winsData, cashData, swordData
	local success, errormessage = pcall(function()
		winsData = dataStore:GetAsync(player.UserId.."-wins")
		cashData = dataStore:GetAsync(player.UserId.."-cash")
		swordData = dataStore:GetAsync(player.UserId.."-sword")
	end)
	
	if success then
		wins.Value = winsData
		cash.Value = cashData
		swordType.Value = swordData
	else
		print("There was an error whilst getting data")
		warn(errormessage)
	end
end)
game.Players.PlayerRemoving:Connect(function(player)
	local success, errormessage = pcall (function()
		dataStore:SetAsync(player.UserId.."-wins",player.leaderstats.Wins.Value)
		dataStore:SetAsync(player.UserId.."-cash",player.leaderstats.Cash.Value)
		dataStore:SetAsync(player.UserId.."-swordType",player.SwordType.Value)
	end)
	if success then
		print("Player data has been saved")
	else
		print("There was an error saving player data")
		warn(errormessage)
	end
end)
game:BindToClose(function()
	wait(3)
end)

like i said, the wins and cash values are saved fine. but when i added the swordtype stuff, it came up with the error.

please could someone help?

2 comments

0

You should put the values in a table and save them like @danklua said. You'll get a warning in the output for too many requests

0

This might be wrong but you may want to look into this, It looks like its saving before any values have been saved, as it's saying there was no value to save.

2 answers

✓ Accepted answer

Try using a table to save your values instead of multiple keys like username-points

example:

local savetable={}
for i,values in pairs(player.leaderstats:GetChildren()) do
savetable[values.Name]=values.Value
end
game:GetService("DataStoreService"):GetDataStore("Saves"):SetAsync(player.UserId,savetable)

you can also add pcall function if you'd like

if you wanted to retrieve the data you can just do

local data=game:GetService("DataStoreService"):GetDataStore("Saves"):GetAsync(player.UserId)
if data~=nil then
for i,v in pairs(data) do
player.leaderstats[i].Value=v
end
end

1 comment

0

thank you for this! this really helped :)

From what I remember you are UNABLED to save string values, and number values so I suggest giving the types a number and putting that number into an int value that's all I can think of you can convert the number into the swords type with a servermodule.

1 comment

0

You should test before answering. Yes, you can store strings to data stores.

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