Why is this printing out nil?

Why does speed boost always print out as nil?

function speedWatch(speed)
	if speed == 24 then
		speed = 1.15
		return speed
	elseif speed == 12 then
		speed = 1
		return speed
	end
end
local speedBoost = speedWatch(humanoid.WalkSpeed)

if speedBoost is equal to nil then clearly your speedWatch function is not returning anything. This means the speed argument is not equal to 24 or 12.#

For reference humanoids have a default walkspeed of 16

2 Likes

In addition to @wf_shā€™s answer, Iā€™d like to add that this problem may be fixed by adding another else statement after it that checks for another WalkSpeed value than 12 and 24. After the change, your code should be looking like this:

function speedWatch(speed)
	if speed == 24 then
		speed = 1.15
		return speed
	elseif speed == 12 then
		speed = 1
		return speed
	else
		return speed
	end
end
function speedWatch(speed)
	if speed == 24 then
		local boostVal = 1.25
		return boostVal
	elseif speed == 16 then
		local boostVal = 1.15
		return boostVal
	else
		local boostVal = 1
		return boostVal
	end
end

?

What are you trying to accomplish?

Would this work in favor of my old script?

This code will never return nil.

1 Like