Creating Bool Value Data Save Help

So I have made a datasave for bool values. But there is this one value that works but does not. So I try to access it in another script and it does not work, and I have been told that the other script that I made was fine, so it is creating it which is the problem. I would like a couple of extra eyes on this. Thanks!
The problem in question is the exicutioneraxe[5] btw

local DataStoreService = game:GetService("DataStoreService")

local ds = DataStoreService:GetDataStore("datatest")

game.Players.PlayerAdded:Connect(function(plr)
	local axes = Instance.new("Folder",plr)
	axes.Name = "axes"
	local Homemadeaxe = Instance.new("BoolValue",axes)
	Homemadeaxe.Name = "Homemadeaxe"
	Homemadeaxe.Value = true
	local Normalaxe = Instance.new("BoolValue",axes)
	Normalaxe.Name = "Normalaxe"
	local Lumerjackaxe = Instance.new("BoolValue",axes)
	Lumerjackaxe.Name = "Lumerjackaxe"
	local Diamondaxe = Instance.new("BoolValue",axes)
	Diamondaxe.Name = "Diamondaxe"
	local Executioneraxe = Instance.new("BoolValue",axes)
	Executioneraxe.Name = "Executioneraxe"
	local Steampunkaxe = Instance.new("BoolValue",axes)
	Steampunkaxe.Name = "Steampunkaxe"
	local Voidaxe = Instance.new("BoolValue",axes)
	Voidaxe.Name = "Voidaxe"
	local Doubleaxe = Instance.new("BoolValue",axes)
	Doubleaxe.Name = "Doubleaxe"

	local success, dataofuser = pcall(ds.GetAsync, ds, plr.UserId)

	if not success then
		plr:Kick("Failed to load data. Please rejoin!")
	end
	if dataofuser ~= nil then 
		print("Found data for " .. plr.Name)
		Homemadeaxe.Value = dataofuser[1]
		Normalaxe.Value = dataofuser[2]
		Diamondaxe.Value = dataofuser[3]
		Lumerjackaxe.Value = dataofuser[4]
		Executioneraxe.Value = dataofuser[5]
		Doubleaxe.Value = dataofuser[6]
		Steampunkaxe.Value = dataofuser[7]
		Voidaxe.Value = dataofuser[8]
		
	else
		print("Replacing no data with new data.")
		Homemadeaxe = true
		Normalaxe = false
		Diamondaxe = false
		Lumerjackaxe = false
		Executioneraxe = false
		Doubleaxe = false
		Steampunkaxe = false
		Voidaxe = false
	end
end)

local function save(player:Player)
	local saveData = {
		[1] = player.axes.Homemadeaxe.Value,
		[2] = player.axes.Normalaxe.Value,
		[3] = player.axes.Diamondaxe.Value,
		[4] = player.axes.Lumerjackaxe.Value,
		[5] = player.axes.Executioneraxe.Value,
		[6] = player.axes.Doubleaxe.Value,
		[7] = player.axes.Steampunkaxe.Value,
		[8] = player.axes.Voidaxe.Value
	}

	local attempt = 0
	local success
	local result

	repeat
		success, result = pcall(ds.UpdateAsync, ds, player.UserId, function(old) return saveData end)
		attempt += 1
	until
	success or attempt == 3
	if not success then
		warn(result)
	end
end



local runService = game:GetService("RunService")
local players = game:GetService("Players")

game:BindToClose(function()
	if runService:IsStudio() or #players:GetPlayers() <= 1 then task.wait(3) return nil end
	for _, player in next, players:GetPlayers(), nil do
		save(player)
	end
	task.wait(3)
end)

game:GetService("Players").PlayerRemoving:Connect(save)

Here is the other script which I was told it was fine if that helps where I access it.

local rp = game:GetService("ReplicatedStorage")
local plr = game:GetService("Players")
local re = game.ReplicatedStorage.OnJoin.Normalaxe
local rr = game.ReplicatedStorage.OnJoin.Diamondaxe
local rt = game.ReplicatedStorage.OnJoin.Lumberjackaxe
local ry = game.ReplicatedStorage.OnJoin.Executioneraxe
local ru = game.ReplicatedStorage.OnJoin.Doubleaxe
local ri = game.ReplicatedStorage.OnJoin.Steampunkaxe
local ro = game.ReplicatedStorage.OnJoin.Voidaxe

plr.PlayerAdded:Connect(function(player: Player)
	local axes = player:WaitForChild("axes")  -- I'm assuming this is some folder in player 
	if not axes then player:Kick("Data could not load, please rejoin.") end 

	local normalAxe = axes:WaitForChild("Normalaxe")
	local diamondAxe = axes:WaitForChild("Diamondaxe")
	local lumberjackAxe = axes:WaitForChild("Lumerjackaxe")
	local ExecutionerAxe = axes:WaitForChild("Executioneraxe")
	local DoubleAxe = axes:FindFirstChild("Doubleaxe")
	local SteampunkAxe = axes:FindFirstChild("Steampunkaxe")
	local VoidAxe = axes:FindFirstChild("Voidaxe")
	print("a")
	if not normalAxe then return end
	if not diamondAxe then return end
	if not lumberjackAxe then return end
	if not ExecutionerAxe then return end
	if not DoubleAxe then return end
	if not SteampunkAxe then return end
	if not VoidAxe then return end
	
	wait(0.01)
	
	if  normalAxe.Value == true then
		re:FireClient(player)
		print("aa")
	end	
	if  diamondAxe.Value == true then
		rr:FireClient(player)
		print("100")
	end	
	if lumberjackAxe.Value == true then
		rt:FireClient(player)
	end	
	if ExecutionerAxe.Value == true then
		ry:FireClient(player)
	end	
	if DoubleAxe.Value == true then 
		ru:FireClient(player)
	end	
	if SteampunkAxe.Value == true then
		ri:FireClient(player)
	end	
	if VoidAxe.Value == true then
		ro:FireClient(player)
	end	
end)                     


2 Likes

Can anyone help my issue that I am having?

2 Likes

I suggest putting all the values and names of the bools in a list and save everything once.

(for the data save enter the list)

2 Likes

I’d like to ask why are you doing all this within one / two scripts, setting own values of the axes. Why not make a module script that can fire when they collect / unlock an axe, reducing the manual labour needed to fill out the script?

2 Likes

May I ask how? I’m new to coding…

2 Likes

I don’t know how to use module scripts, so the fist script makes it and saves it the second makes it so when the player joins is fires something to do something on the players side and for some reason the executioner axe does not work…

2 Likes

Ah I see, let me try fix it for you, i’ll see what I can do within the two scripts

2 Likes

@TheReal_MrMonkey Okay, so I’ve divided these into a module script and also a main script,

local DataStoreService = game:GetService("DataStoreService")
local ds = DataStoreService:GetDataStore("datatest")
local Players = game:GetService("Players")
local AxesModule = require(game:GetService("ReplicatedStorage").AxesModule)

local function createAxesFolder(player)
	local axes = Instance.new("Folder", player)
	axes.Name = "axes"
	for _, axeName in ipairs(AxesModule.AxesList) do
		local axeValue = Instance.new("BoolValue", axes)
		axeValue.Name = axeName
	end
end

local function loadData(player)
	local success, dataofuser = pcall(ds.GetAsync, ds, player.UserId)
	if not success then
		player:Kick("Failed to load data. Please rejoin!")
	end
	return dataofuser or {}
end

local function saveData(player)
	local saveData = {}
	for _, axeName in ipairs(AxesModule.AxesList) do
		table.insert(saveData, player.axes[axeName].Value)
	end
	local success, result = pcall(ds.UpdateAsync, ds, player.UserId, function(old) return saveData end)
	if not success then
		warn(result)
	end
end

local function onPlayerAdded(player)
	createAxesFolder(player)
	local dataofuser = loadData(player)
	for i, axeName in ipairs(AxesModule.AxesList) do
		player.axes[axeName].Value = dataofuser[i] or false
	end
end

local function onPlayerRemoving(player)
	saveData(player)
end

Players.PlayerAdded:Connect(onPlayerAdded)
Players.PlayerRemoving:Connect(onPlayerRemoving)

game:BindToClose(function()
	if game:GetService("RunService"):IsStudio() or #Players:GetPlayers() <= 1 then
		task.wait(3)
		return
	end
	for _, player in ipairs(Players:GetPlayers()) do
		saveData(player)
	end
	task.wait(3)
end)

Here is the main script above!

Furthermore to ensure that you dont have to create your own bool values within each and every script, i’ve created a module script in which the main script cycles through and creates the bool values automatically.

Here is the module script below!
Ensure you create a module script within replicated storage and call it AxesModule

local Axes = {}

Axes.AxesList = {
    "Homemadeaxe",
    "Normalaxe",
    "Lumerjackaxe",
    "Diamondaxe",
    "Executioneraxe",
    "Doubleaxe",
    "Steampunkaxe",
    "Voidaxe"
}

return Axes

After doing this you should be able to change the values of the bool values and it should save when the player leaves!

DM if you have any issues, P.S I have not tested this as I dont have the game’s framework

3 Likes

I’m getting home in like 17 min so I’ll test and where would I put this module script?

2 Likes

ReplicatedStorage :slight_smile:
Any other problems don’t be scared to reply!

2 Likes

Ok your the best I’ll get back to you if anything does not work and if it does work it will be the solution

3 Likes

I’m happy to have helped thanks for applying my work as the solution, any problems in the future, ping me, I’ll try my best to help! :grin:

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.