Error: Attempt to call a nil value

I’m trying to get a waving system to work, using https://devforum.roblox.com/t/how-to-get-wave-height-at-a-specific-position/30672/5 , but I ran into a problem. When I tried to call a function, it gave me an error. I tried searching for stuff but really don’t know the cause of this.

local waveHeight = workspace.Terrain.WaterWaveSize * 2;
local waveWidth = 85;
--while _G.GetWaterWaveCycle == nil do wait()
--end

while true do
	wait()
	if _G.GetWaterWaveCycle ~= nil then
	local d = _G.GetWaterWaveCycle()
	print(d)
	_G.GetWaterWaveCycle()
	height(script.Parent.Position.X, script.Parent.Position.Z, d) -- This is the error part!!
	end
end

function height(x, z, cycle)
	print("Error check")
	return waveHeight * math.sin(2*math.pi*x/waveWidth + cycle + math.pi/2)
		* math.sin(2*math.pi*z/waveWidth);
end

and the other script:

if not _G.GetWaterWaveCycle then
	assert(workspace.Terrain.WaterWaveSpeed==0, "WaveSpeed staat op 0");
	local cycleAtLastSet = 0; 
	local tickAtLastSet = 0; 
	local speedAtLastSet = 0; 

	function _G.GetWaterWaveCycle()
		return math.pi * 2 * ((cycleAtLastSet + (tick() - tickAtLastSet)
			* workspace.Terrain.WaterWaveSpeed / 85) % 1);
	end

	workspace.Terrain.Changed:connect(function(prop)
		if prop == "WaterWaveSpeed" then
			cycleAtLastSet = (cycleAtLastSet + 
				(tick() - tickAtLastSet) * speedAtLastSet / 85) % 1;
			print("CYCLE STAAT: ", cycleAtLastSet);
			tickAtLastSet = tick();
			speedAtLastSet = workspace.Terrain.WaterWaveSpeed;
		end
	end)
end
2 Likes

You’ve declared the height function after your while loop, so it doesn’t exist when you attempt to call it from within the loop.

This means height is technically nil at this point, resulting in the error you’re seeing. Simply move the function so it’s declared before the loop and the error should be resolved.

6 Likes

Oh bruh didn’t see that :man_facepalming:t3: thanks man!

3 Likes