How do I fix this issue with detecting IntValue changes?

I am having an error when I try to detect IntValue changes for a player.

local coinStore = game:GetService("DataStoreService"):GetDataStore("coinsv1")
local jumpAmountStore = game:GetService("DataStoreService"):GetDataStore("jumpv1")
local levelStore = game:GetService("DataStoreService"):GetDataStore("levelv1")

game.Players.PlayerAdded:Connect(function(p)
	local leaderstats = Instance.new("Folder", p)
	local coins = Instance.new("IntValue", leaderstats)
	local levels = Instance.new("IntValue", leaderstats)
	local jumpAmount = Instance.new("IntValue", leaderstats)
	leaderstats.Name = "leaderstats"
	coins.Name = "Coins"
	levels.Name = "Level"
	jumpAmount.Name = "Jumps"
	coins.Value = 0
	levels.Value = 1
	jumpAmount.Value = 0
	
	local jumpPower
	p.CharacterAdded:Connect(function(c)
		jumpPower = c.Humanoid.JumpPower
		c.Humanoid.JumpPower = jumpPower
	end)
	
	local data
	local datatwo
	local datathree
	local success, errormessage = pcall(function()
		data = coinStore:GetAsync(p.UserId.."-coins")
		datatwo = jumpAmountStore:GetAsync(p.UserId.."-jumps")
		datathree = levelStore:GetAsync(p.UserId.."-level")
	end)
	
	if success then
		coins.Value = data
		levels.Value = datathree
		jumpAmount.Value = datatwo
	else
		print(errormessage)
	end
	
	local recentcoins
	jumpAmount.Changed:Connect(function()
		if jumpAmount.Value == datatwo + 20 then
			levels.Value = levels.Value + 1
			recentcoins = jumpAmount.Value
		elseif jumpAmount.Value == recentcoins + 20 then
			levels.Value = levels.Value + 1
			recentcoins = jumpAmount.Value	
		end
	end)
	
	local recentjumps
	jumpAmount.Changed:Connect(function()
		if jumpAmount.Value == datatwo + 10 then
			local jumps = jumpAmount.Value / 10
			jumps = jumps + 50
			local cjump = p.Character.Humanoid.JumpPower
			cjump = jumps
			recentjumps = jumpAmount.Value
		elseif jumpAmount.Value == recentjumps + 10 then
			local jumps = jumpAmount.Value / 10
			jumps = jumps + 50
			local cjump = p.Character.Humanoid.JumpPower
			cjump = jumps
			recentjumps = jumpAmount.Value	
		end
	end)
end)

The error is at line 46 and 60. The error is attempt to perform arithmetic (add) on nil and number.

This may be a simple error, as I’ve been coding for about 6 and a half hours now, so I may have just made a typo.

Thanks!

At line 46 (ish?) you have this problem


Where recentcoins may not have been defined yet if the first part of the if statement doesn’t happen first, and the elseif is being evaluated as jumpAmount.Value == nil + 20
The same thing happens with recentjumps

How should I fix it?

{30CHAR 30CHAR}

Give them an initial value when you define them
I’m guessing they should start at 0

local recentcoins = 0
local recentjumps = 0