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