How would I go on saving weapon unlocks? I don’t use tools but instead use my own FPS Engine.
I feel like creating a bool value for each gun in the game would be unoptimized.
As far as I know, there are no topics I could find relating to my question.
I’m quite new to data storing but I can only really save basic information such as coins/kills/etc. Is there an optimized way of saving guns a player has unlocked?
Tables can be useful for saving data. To add onto the post above, I would make a folder under the player (not leaderstats) that contains BoolValues each named off a gun they’ve unlocked.
-- in a PlayerAdded function
local gunsUnlocked = Instance.new("Folder", player) -- hidden folder under the player that will contain the names of all the guns they unlocked
gunsUnlocked.Name = "gunsUnlocked"
local loadData = {}
local success, fail = pcall(function()
loadData = dataStore:GetAsync(player.UserId .. "-data") -- use the right data key name
end)
if success then
coins.Value = loadData[1]
kills.Value = loadData[2]
for i, v in pairs(loadData[3]) do -- read off the gunsUnlocked table
local newGun = Instance.new("BoolValue", gunsUnlocked)
newGun.Name = v -- the saved table of guns contains strings
end
else
warn("Error loading for " .. player.Name .. ". " .. fail)
end
-- in a PlayerRemoving function
local saveData = {}
local gunsUnlocked = {}
for i, v in pairs(player.gunsUnlocked:GetChildren()) do
table.insert(gunsUnlocked, v.Name) -- add the gun names to the gunsUnlocked table
end
table.insert(saveData, player.leaderstats.Coins.Value) -- insert the values youre saving into the main save table
table.insert(saveData, player.leaderstats.Kills.Value)
table.insert(saveData, gunsUnlocked) -- saving will allow tables inside tables
local success, fail = pcall(function()
dataStore:SetAsync(player.UserId .. "-data", saveData) -- use the right data key name
end)
if success then
-- print("Save success!")
else
warn("Error saving for " .. player.Name .. ". " .. fail)
end
Then, scripts can easily check if the player owns the gun under their gunsUnlocked folder, as well as add to the guns they’ve unlocked.
local gunsUnlocked = game.Players.playerName.gunsUnlocked
if gunsUnlocked:FindFirstChild("pistol") then
-- they have the pistol
end
function unlockGun(gunName)
local newGunValue = Instance.new("BoolValue", gunsUnlocked)
newGunValue.Name = gunName
end
Also, this code will error if the player doesnt have any saved data, but then everything works fine after that. If you decide to rename any of the guns after adding them, then the code will treat it as an entirely separate gun.
I don’t claim to be an expert in coding, this is just the way I would do it that I thought up of. Sorry if this post is too long or complicated.