Help with strings getting corrupted in the datastore

Hello im trying to make a saving system to save keybinds as strings in my game, the issue is that sometimes when i join it sets the value to a empty string and then of course it saves that empty string and keeps it that way.

Here is the code:

local dataStoreService = game:GetService("DataStoreService")
local data = dataStoreService:GetDataStore("Data")

	local primaryKeybind ="StringValue",player)
	primaryKeybind.Name = "PrimaryKeybind"
	local secondaryKeybind ="StringValue",player)
	secondaryKeybind.Name = "SecondaryKeybind"
	local meleeKeybind ="StringValue",player)
	meleeKeybind.Name = "MeleeKeybind"
	local explosiveKeybind ="StringValue",player)
	explosiveKeybind.Name = "ExplosiveKeybind"
	local firemodeKeybind ="StringValue",player)
	firemodeKeybind.Name = "FiremodeKeybind"
	local primaryKeybindValue = nil
	local secondaryKeybindValue = nil
	local meleeKeybindValue = nil
	local explosiveKeybindValue = nil
	local firemodeKeybindValue = nil
	local success = pcall(function()
		primaryKeybindValue = data:GetAsync(player.UserId.."PrimaryKeybind")
		secondaryKeybindValue = data:GetAsync(player.UserId.."SecondaryKeybind")
		meleeKeybindValue = data:GetAsync(player.UserId.."MeleeKeybind")
		explosiveKeybindValue = data:GetAsync(player.UserId.."ExplosiveKeybind")
		firemodeKeybindValue = data:GetAsync(player.UserId.."FiremodeKeybind")
	if success == true then
		primaryKeybind.Value = primaryKeybindValue or tostring(Enum.KeyCode.One)
		secondaryKeybind.Value = secondaryKeybindValue or tostring(Enum.KeyCode.Two)
		meleeKeybind.Value = meleeKeybindValue or tostring(Enum.KeyCode.F)
		explosiveKeybind.Value = explosiveKeybindValue or tostring(Enum.KeyCode.G)
		firemodeKeybind.Value = firemodeKeybindValue or tostring(Enum.KeyCode.V)

Please let me know of a way to fix this thank you :slight_smile:

Ok, so, first, my recommendations:

  1. Save a table to the data store instead of multiple for almost the same purpose, it makes it easier for access and you don’t have to call multiple data stores with different keys.
  2. Use pcalls for SetAsync, if Roblox’s servers are down, your script and the player’s data is done

Ok, so that’s out of the way. The reason why the strings are sometimes blank is because if the data store fetch was unsuccessful, there’s no other default values for the string to use

Ok thanks ill try this in the morning :smiley: also what’s the point of pcalling it for the setasync because if they leave then they’ve left right?

Yea, but SetAsync overwrites the previous value, so if it fails or the value is blank, that will lead to some issues

If you don’t wanna do a pcall then do :UpdateAsync() instead of :SetAsync(). Also you don’t have to do a table, there’s just less places to go wrong. If you’re sure your script is foolproof you don’t have to save to a table. Hope this helps!

Yea but its an event if there was an error nothing will happen also my keybinds havent currupted yet so it looks like it worked thanks! :smiley: