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

How do I properly save stringvalues with datastores?

So in my game, I've recently been working on a system to save the weapon you have equipped that works in the same script as my saving script for all the player's stats, but I'm using a stringvalue that keeps the name of the weapon equipped and looks for the name of the weapon in ReplicatedStorage. (I'm using stringvalues because datastores can't save objectvalues.) However, there's one problem. The saving doesn't work! Here's the script.

local DSService = game:GetService('DataStoreService'):GetDataStore('ZA3TestData')
game.Players.PlayerAdded:connect(function(plr)
	-- Define variables
	local uniquekey = 'id-'..plr.userId
	local leaderstats = Instance.new('Folder', plr)
	local savevalue1 =  Instance.new('IntValue')
	local savevalue2 = Instance.new('IntValue')
	local savevalue3 = Instance.new('IntValue', plr)
	local savevalue4 = Instance.new('IntValue')
	local savevalue5 = Instance.new('StringValue', plr)
	leaderstats.Name = 'leaderstats'
	savevalue4.Parent = leaderstats
	savevalue1.Parent = leaderstats
	savevalue2.Parent = leaderstats
	savevalue1.Name = 'Level'
	savevalue2.Name = 'Prestige'
	savevalue3.Name = "XP"
	savevalue4.Name = "Kills"
	savevalue5.Name = "SavedWeapon"

	-- GetAsync
	local GetSaved = DSService:GetAsync(uniquekey)
	if GetSaved then
		savevalue1.Value = GetSaved[1]
		savevalue2.Value = GetSaved[2]
		savevalue3.Value = GetSaved[3]
		savevalue4.Value = GetSaved[4]
		savevalue5.Value = GetSaved[5] or "Brick"
	else
		local NumbersForSaving = {savevalue1.Value, savevalue2.Value, savevalue3.Value, savevalue4.Value, savevalue5.Value}
		DSService:SetAsync(uniquekey, NumbersForSaving)
	end
end)

game.Players.PlayerRemoving:connect(function(plr)
local uniquekey = 'id-'..plr.userId
local Savetable = {plr.leaderstats.Level.Value, plr.leaderstats:FindFirstChild('Prestige').Value, plr.XP.Value, plr.leaderstats.Kills.Value, plr.SavedWeapon.Value}
DSService:SetAsync(uniquekey, Savetable)
	
	
end)

Also, when I load the game with the script, this is the error I get:

ServerScriptService.Player Stats:28: bad argument #3 to 'Value' (string expected, got nil)

Thanks!

1 answer

Hello!

I might see the problem! It's the "String Value". I'd would suggest to create a folder. And add all weapons in it. And make the "String Value" to a "Int Value".

The idea is. That you make a map with saved weapons. And every weapon will have a ID. And not a name. Working with "Int Values" are much easier.

Just a tip! Hope you will get it fixed.

[NOTE] You problem is with the "String Value". ^ Those are tips.

Good luck! Greetings from me.

2 comments

0

All my weapons have a name because in my custom weapon hotbar GUI it shows the name of the weapon above the button to equip it, but I might just remove that for this. I'll see how it goes, thanks!

0

U could add a script to define every ID to a weapon. It's more securely then string value's.

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