Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

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.

1 answer

On line 41, you set recentcoins variable to nil . So on line 46 and 60, it tries to add 20 and 10 to a nil value, causing the error. Set recentcoins to 0 instead of just nil.

EDIT: It's also the case with line 52. Fix that too.

This discussion is preserved as it was. New answers, comments, and votes are closed.