Sometimes function wont cast a ray for unknown reasons

Currently the script does everything completely fine BESIDES actually making the ray cast for some reason

It creates the ray and other times it doesn’t and I’m unsure as to why


In the output you can see the warnings of a ray not being created despite the function clearly running

Here’s the part of the script that’s causing problems

local startingPoint
local midPoint
local endPoint

local function rayCast(firePoint: Vector3, fireDest: Vector3)
	print("hello im attempting to ray cast now")
	local dir = fireDest - firePoint
	local params = RaycastParams.new()
	params.FilterDescendantsInstances = {script.Parent.Parent}
	params.FilterType = Enum.RaycastFilterType.Exclude
	params.IgnoreWater = true

	local _ray = workspace:Raycast(firePoint, dir, params)
	if _ray then
		if startingPoint then
			startingPoint:Remove()
			midPoint:Remove()
			endPoint:Remove()
		end
		
		startingPoint = Instance.new("Part")
		startingPoint.Transparency = 0.65
		startingPoint.Color = Color3.new(0.333333, 1, 0)
		startingPoint.Material = Enum.Material.Neon
		startingPoint.Anchored = true
		startingPoint.Size = Vector3.new(0.075,0.075,0.075)
		startingPoint.CanCollide = false
		startingPoint.CanQuery = false
		startingPoint.Parent = workspace
		startingPoint.Position = firePoint

		midPoint = Instance.new("Part")
		midPoint.Transparency = 0.65
		midPoint.Color = Color3.new(1, 0.666667, 0)
		midPoint.Material = Enum.Material.Neon
		midPoint.Anchored = true
		midPoint.CFrame = CFrame.lookAt(firePoint, fireDest)*CFrame.new(0, 0, -(firePoint - fireDest).Magnitude/2)
		midPoint.Size = Vector3.new(0.055, 0.055, (firePoint - fireDest).Magnitude)
		midPoint.CanCollide = false
		midPoint.CanQuery = false
		midPoint.Parent = workspace

		endPoint = Instance.new("Part")
		endPoint.Transparency = 0.65
		endPoint.Color = Color3.new(1, 0, 0)
		endPoint.Material = Enum.Material.Neon
		endPoint.Anchored = true
		endPoint.Size = Vector3.new(0.075,0.075,0.075)
		endPoint.CanCollide = false
		endPoint.CanQuery = false
		endPoint.Parent = workspace
		endPoint.Position = fireDest
	else
		warn("no ray")
	end
	if _ray and _ray.Instance then
		print("yay we got a valid ray")
		local rayInstance = _ray.Instance:: BasePart
		print(rayInstance)

		if rayInstance.Parent:FindFirstChildOfClass("Humanoid") then
			print("nice we found a humanoid")
			local hum = rayInstance.Parent:FindFirstChildOfClass("Humanoid")
			print(rayInstance.Name)
			print(hum.Name)
			return rayInstance,hum
		end
	else
		print("got nothing")
		return nil, nil
	end
end

-- script
script.Parent.Equipped:Connect(function()
	sound_equip:Play()
	connection_shoot = event_fire.OnServerEvent:Connect(function(plr, firePoint: Vector3, fireDest: Vector3)
		print("hello I got the damage request")
		coroutine.wrap(effects)()
		local hitObject,hum = rayCast(firePoint, fireDest)
		print(hitObject.Name)
		print(hum.Name)
		
		if hitObject ~= nil and hum ~= nil then
			print("they were both valid!")
			print("hit: "..hitObject.Name..", hum: "..hum.Name)
			attemptDamage(hitObject, hum)
		end
	end)
	
	script.Parent.Unequipped:Connect(function()
		sound_equip:Stop()
		connection_shoot:Disconnect()
	end)
end)
2 Likes

I found out the actual problem!

I had to multiply by 300 here to increase the total range I think?

local _ray = workspace:Raycast(firePoint, dir*300, params)

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