Raycast Problem

TestService: Exception thrown in your RayHit event handler: Argument 1 missing or nil

That’s the error I’m getting while firing my remote event which is an raycast event.

Here’s the more important part of the code:

local castParams = RaycastParams.new()
	castParams.FilterType = Enum.RaycastFilterType.Blacklist
	castParams.IgnoreWater = true

	local castBehavior = FastCast.newBehavior()
	castBehavior.RaycastParams = castParams
	castBehavior.Acceleration = Vector3.new(0, -workspace.Gravity, 0)
	castBehavior.AutoIgnoreContainer = false
	castBehavior.CosmeticBulletContainer = bulletsFolder
	castBehavior.CosmeticBulletTemplate = bulletTemplate

	local function onEquipped()
		castParams.FilterDescendantsInstances = {viewmodel, bulletsFolder, character}
	end

	local function onLengthChanged(cast, lastPoint, direction, length, velocity, bullet)
		if bullet then 
			local bulletLength = bullet.Size.Z/2
			local offset = CFrame.new(0, 0, -(length - bulletLength))
			bullet.CFrame = CFrame.lookAt(lastPoint, lastPoint + direction):ToWorldSpace(offset)
		end
	end

	local function onRayHit(cast, result, velocity, bullet)
		local hit = result.Instance

		local character = hit:FindFirstAncestorWhichIsA("Model")
		if character and character:FindFirstChild("Humanoid") and playersmode then
			local player3 = game.Players:GetPlayerFromCharacter(character)
			if player3 then
				
				if player3.TeamColor ~= player.TeamColor then
					
					character.Humanoid:TakeDamage(damage)
					
				end
				
			else
				-- do nothing
			end
		end
		
		if character and character:FindFirstChild("Humanoid") and not playersmode then
			
			character.Humanoid:TakeDamage(damage)
			
		end
		
		game:GetService("Debris"):AddItem(bullet, 1.2)
	end

	local function fire(player3, mousePosition)
		local origin = firePoint
		local direction = (mousePosition - origin).Unit

		caster:Fire(origin, direction, 1000, castBehavior)
	end

Anyone got a solution?

I can’t make any further conclusions due to lack of information, but it appears that the issue is because the variable damage is undefined (i.e., nil).

1 Like

Ah, alright. Will look into it.

Yep! That was the problem. Didn’t realize my module didn’t contain the “Damage” value which I thought I already added to the module then I just passed the value to the remote event blindly. Thank you!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.