When test my script, my DataStore script does not work. Furthermore there is an error, it is attempt to index nil with 'money' (see the script below):

The error is in the load() function at line 20, any help would be gladly appreciated.

local DataStoreService = game:GetService("DataStoreService")
local userDataStore = DataStoreService:GetDataStore("UserDataStore")

local starterData = {
	money = 100,
	vehicles = {}
}

function save(player)
	local success, err = pcall(function() 
		userDataStore:SetAsync("Player_" .. player.UserId, 
		{
			money = game.Workspace.PlayerStats["Player_" .. player.UserId].NoobMarks.Value
		})
	end)
	if err then print("Player data failed to save for " .. player.Name) end
end

function load(player)
	local playerStats = game:GetService("ReplicatedStorage"):WaitForChild("Templates"):WaitForChild("PlayerStatsTemplate"):Clone()
	playerStats.Name = "Player_" .. player.UserId
	playerStats.Parent = game.Workspace.PlayerStats
	
	local data
	local success = pcall(function()
		data = userDataStore:GetAsync(tostring("Player_" .. player.UserId)) 
	end)
	
	-- Load data
	if success then
		playerStats.NoobMarks.Value = data.money
	else
		print("Player data failed to load for " .. player.Name)
	end
end

game.Players.PlayerAdded:Connect(load)
game.Players.PlayerRemoving:Connect(save)

Thanks in advance.

1 comment

0

Under your if success statement do if data ~= nil then playerStats.NoobMarks.Value = data.money end