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")

game.Players.PlayerAdded:Connect(function(player)
	local primaryKeybind = Instance.new("StringValue",player)
	primaryKeybind.Name = "PrimaryKeybind"
	local secondaryKeybind = Instance.new("StringValue",player)
	secondaryKeybind.Name = "SecondaryKeybind"
	local meleeKeybind = Instance.new("StringValue",player)
	meleeKeybind.Name = "MeleeKeybind"
	local explosiveKeybind = Instance.new("StringValue",player)
	explosiveKeybind.Name = "ExplosiveKeybind"
	local firemodeKeybind = Instance.new("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")
	end)
	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)
	end
end)
game.Players.PlayerRemoving:Connect(function(player)
	data:SetAsync(player.UserId.."PrimaryKeybind",player.PrimaryKeybind.Value)
	data:SetAsync(player.UserId.."SecondaryKeybind",player.SecondaryKeybind.Value)
	data:SetAsync(player.UserId.."MeleeKeybind",player.MeleeKeybind.Value)
	data:SetAsync(player.UserId.."ExplosiveKeybind",player.ExplosiveKeybind.Value)
	data:SetAsync(player.UserId.."FiremodeKeybind",player.FiremodeKeybind.Value)
end)

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:

2 Likes