Trying to Save JumpPower in Datastore

In my game you can purchase JumpPower upgrades, however though I do not want to make a IntValue that is saved inside a folder called PlayerStats because I feel like there is just an easier approach without having to create a whole bunch of other scripts. Here is the Datastore code:

local datastoreservice = game:GetService('DataStoreService')
local datastore = datastoreservice:GetDataStore("FAKENAME")

local function playerAdded(player)
	local Data = datastore:GetAsync(player.UserId)
	if Data then
		-- Defaults
		player.leaderstats.Wins.Value = Data["Wins"]
		player.leaderstats.Cash.Value = Data["Cash"]
		-- SpeedBoost
		player.Abilities.SpeedBoost.Value = Data["SB"]
		player.Abilities.SpeedBoost.Time.Value = Data["SBT"]
		player.Abilities.SpeedBoost.Cooldown.Value = Data["SCD"]
		-- Blindable
		player.Abilities.Blindable.Value = Data["BL"]
		player.Abilities.Blindable.Time.Value = Data["BLT"] 
		player.Abilities.Blindable.Cooldown.Value = Data["BCD"]
		player.Character:WaitForChild("Humanoid").JumpPower = Data["JP"] -- Error Occurs Here
	end
end

local function playerRemoved(player)
	local success, errorMsg = pcall(function()
		datastore:UpdateAsync(player.UserId, function()
			return {
				-- Defaults
				Wins = player.leaderstats.Wins.Value,
				Cash = player.leaderstats.Cash.Value,
				-- SpeedBoost
				SB = player.Abilities.SpeedBoost.Value,
				SBT = player.Abilities.SpeedBoost.Time.Value,
				SCD = player.Abilities.SpeedBoost.Cooldown.Value,
				-- Blindable
				BL = player.Abilities.Blindable.Value,
				BLT = player.Abilities.Blindable.Time.Value,
				BCD = player.Abilities.Blindable.Cooldown.Value,
				-- Humanoid Saves
				JP = player.Character:WaitForChild("Humanoid").JumpPower, -- Second Error Occurs Here
			}
		end)
	end)
	if errorMsg then
		print(errorMsg)
	end
end

game.Players.PlayerAdded:Connect(playerAdded)
game.Players.PlayerRemoving:Connect(playerRemoved)

game:BindToClose(function()
	wait(3)
end)

Here’s the error that occurs:

ServerScriptService.Stat Handler.Datastore:18: attempt to index nil with 'WaitForChild'  -  Server - Datastore:18

Fixed:

local datastoreservice = game:GetService('DataStoreService')
local datastore = datastoreservice:GetDataStore("DrPhilGame")

local function playerAdded(player)
	local character = player.Character or player.CharacterAdded:Wait() -- Added This
	local Data = datastore:GetAsync(player.UserId)
	if Data then
		-- Defaults
		player.leaderstats.Wins.Value = Data["Wins"]
		player.leaderstats.Cash.Value = Data["Cash"]
		-- SpeedBoost
		player.Abilities.SpeedBoost.Value = Data["SB"]
		player.Abilities.SpeedBoost.Time.Value = Data["SBT"]
		player.Abilities.SpeedBoost.Cooldown.Value = Data["SCD"]
		-- Blindable
		player.Abilities.Blindable.Value = Data["BL"]
		player.Abilities.Blindable.Time.Value = Data["BLT"] 
		player.Abilities.Blindable.Cooldown.Value = Data["BCD"]
		-- Humanoid Saves
		character:WaitForChild("Humanoid").JumpPower = Data["JP"]
	end
end

local function playerRemoved(player)
	local character = player.Character or player.CharacterAdded:Wait() -- Added
	local success, errorMsg = pcall(function()
		datastore:UpdateAsync(player.UserId, function()
			return {
				-- Defaults
				Wins = player.leaderstats.Wins.Value,
				Cash = player.leaderstats.Cash.Value,
				-- SpeedBoost
				SB = player.Abilities.SpeedBoost.Value,
				SBT = player.Abilities.SpeedBoost.Time.Value,
				SCD = player.Abilities.SpeedBoost.Cooldown.Value,
				-- Blindable
				BL = player.Abilities.Blindable.Value,
				BLT = player.Abilities.Blindable.Time.Value,
				BCD = player.Abilities.Blindable.Cooldown.Value,
				-- Humanoid Saves
				JP = character:WaitForChild("Humanoid").JumpPower, 
			}
		end)
	end)
	if errorMsg then
		print(errorMsg)
	end
end

game.Players.PlayerAdded:Connect(playerAdded)
game.Players.PlayerRemoving:Connect(playerRemoved)

game:BindToClose(function()
	wait(3)
end)

It failed to get the players character because the character was not loaded in. Minor mistake. Problem solved.

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