Boolean data save

I have a error and i do not know how to fix it. So ther error is, 1 is not a valid member of dataStore “datastoreservice.datatest” It is on line 20 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"
	local Normalaxe = Instance.new("BoolValue",axes)
	Normalaxe.Name = "Normalaxe"

	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 = ds[1]
		Normalaxe.Value = ds[2]
	else
		print("Replacing no data with new data.")
		Homemadeaxe = true
		Normalaxe = false
	end
end)

local function save(player:Player)
	local saveData = {
		[1] = player.axes.Homemadeaxe.Value,
		[2] = player.axes.Normalaxe.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)
Original reply

I noticed that you are using the pcall and the datastore functions improperly. Try replacing the loading pcall with this:-

local success, dataofuser = pcall(function()
    return ds:GetAsync(plr.UserId.."ExampleKey")
end)

and the saving pcall with this:

success, result = pcall(function()
    ds:SetAsync(plr.UserId.."ExampleKey", saveData)
end)

The two main issues you made were

  1. Not specifying the datastore key
  2. Using a . instead of a : when using GetAsync and UpdateAsync (or SetAsync in my example)

Edit: I didn’t realize that the pcall method you used was possible

The way the OP did it originally works the same


@TheReal_MrMonkey The problem is here:

This should be:

Homemadeaxe.Value = dataofuser[1]
Normalaxe.Value = dataofuser[2]
2 Likes