Datastore bug. Require help!

Can somebody help me with this datastore bug? I don’t know how to fix it

local Players = game:GetService("Players")
local RunService = game:GetService("RunService")
local isStudio = RunService:IsStudio()
local DataStoreService = game:GetService("DataStoreService")
local ds = DataStoreService:GetDataStore("LeaderStatSave")

local temporalData = {}
local abilities = {
	"Boost",
	"Heal",
	"Engineer"
}

Players.PlayerAdded:Connect(function(plr)
	local leader = Instance.new("Folder")
	leader.Name = "leaderstats"

	local private = Instance.new("Folder")
	private.Name = "PrivateStats"

	local Coins = Instance.new("NumberValue", leader)
	Coins.Name = "Coins"

	local Wins = Instance.new("NumberValue", leader)
	Wins.Name = "Wins"

	leader.Parent = plr

	temporalData[plr.UserId] = ds:GetAsync(plr.UserId) or {}

	if temporalData[plr.UserId].Abilities == nil then
		temporalData[plr.UserId].Abilities = {} -- Error
	end

	for _, ability in ipairs(abilities) do
		if temporalData[plr.UserId].Abilities[ability] == nil then
			temporalData[plr.UserId].Abilities[ability] = false
		end
		local bool = Instance.new("BoolValue")
		bool.Name = ability
		bool.Value = temporalData[plr.UserId].Abilities[ability]
		bool:GetPropertyChangedSignal("Value"):Connect(function()
			temporalData[plr.UserId].Abilities[ability] = bool.Value
		end)
		bool.Parent = private
	end

	private.Parent = plr

	Coins.Value = temporalData[plr.UserId].Coins or 0
	Coins:GetPropertyChangedSignal("Value"):Connect(function()
		temporalData[plr.UserId].Coins = Coins.Value
	end)

	Wins.Value = temporalData[plr.UserId].Wins or 0
	Wins:GetPropertyChangedSignal("Value"):Connect(function()
		temporalData[plr.UserId].Wins = Wins.Value
	end)

	plr.AncestryChanged:Connect(function()
		if plr:IsDescendantOf(game) or temporalData[plr.UserId] == nil then
			return
		end
		if isStudio then
			print("Data not saved because it runs in Studio")
			return
		end
		local s, err = pcall(function()
			ds:UpdateAsync(plr.UserId, function()
				return temporalData[plr.UserId]
			end)
		end)
		if s ~= true then
			warn("Failed saving data for player: " .. plr.Name .. ", key: " .. plr.UserId .. "\n" .. tostring(err))
		else
			print("Data saved for player: " .. plr.Name)
		end
		temporalData[plr.UserId] = nil
	end)
end)

Error:

What is the issue you are having with it?
I tested the script and it works on my machine

It’s alright. it got fixed. I accidently made this whilst i was crashing from devforum

1 Like