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

My fall damage script keeps giving me several different errors?

I'm attempting to make a fall damage script for my game, however when I try to calculate the amount of time the player has been falling it prints 'nil'

I tried to use a while loop and a repeat loop but both printed the same error: Workspace.Louscascicly.FallDamageScript:30: attempt to perform arithmetic (mul) on nil and number

Here is my script:

local PlayerService = game:GetService("Players")
local Player = PlayerService.LocalPlayer
local Character = Player.Character
local Humanoid = Character:WaitForChild("Humanoid")

local FreefallTime

local DAMAGE_PER_SECOND = 5
local FATAL_SECONDS = 15
local MINIMUM_SECONDS = 3

Humanoid.StateChanged:Connect(function(state)
	if state == Enum.HumanoidStateType.Freefall then
		repeat
			FreefallTime = FreefallTime + 1
			print(FreefallTime)
		until
			state == Enum.HumanoidStateType.Landed
		
	elseif state == Enum.HumanoidStateType.Landed then
		print("Landed")
		print(FreefallTime)
		if FreefallTime > MINIMUM_SECONDS then
			if FreefallTime > FATAL_SECONDS then
				Humanoid:TakeDamage(1000000000000)
			else
				local RefinedDamage = FreefallTime * DPS
				Humanoid:TakeDamage(RefinedDamage)
			end
		end
	end
end)

1 answer

✓ Accepted answer

You are attempting to perform math on nil, which is not possible. You will need to assign the variable to an int.

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