Raycast weapon breaks after shooting at the sky

I’ve seen other posts asking about this very issue, but none of them have really been helpful to me. I don’t know if I didn’t follow correctly or if they actually weren’t helpful, but regardless, I would like to know how I can stop my raycast weapon from breaking after shooting at the sky.

I get no errors in the output console or anything. Another post has implied that by shooting at the sky, you are giving the raycast too great of a distance to cover, and that you’d have to limit this. I tried doing that. It did not solve my problem.

This is my raycast. It’s not the entire code, but I believe this is the only part that really matters right now.

local origin = gun.Handle.Position
local direction = (position - origin).Unit*range
local result = workspace:Raycast(origin, direction)

local intersection = result and result.Position or origin + direction * 64
local distance = (origin - intersection).Magnitude
print(distance)

local bullet_clone = ServerStorage.Bullet:Clone()
bullet_clone.Size = Vector3.new(0.1, 0.1, distance)
bullet_clone.CFrame = CFrame.new(origin, intersection)*CFrame.new(0, 0, -distance/2)
bullet_clone.Parent = workspace

There’s definitely something I’m not seeing here, I assume. The raycast works perfectly well if I just don’t shoot at the sky, so I don’t think the problem lies in the weapon or whatever.

Edit: I would like to add that, when I shoot at the sky, a very short raycast is created and the distance printed into the console is the same value as the length of the raycast. Afterwards, the weapon stops working entirely; the clientside part of it still works but the serverside script just breaks entirely without a single error in the output.

2 Likes

I still can’t figure out how to fix this. I’m messing around with the range and nothing seems to work.

Hi, when hitting something with a raycast if you want to get the position of something hit, you must get the instance and then the position, e.g.

if RaycastResult and RaycastResult.Instance then
local Hit = RaycastResult.Instance
local HitPosition = Hit.Position
end

Hope this helps!

2 Likes

I have something like that in my code, for tagging and damaging whatever humanoid the raycast lands on,

if result then
			local part = result.Instance
			local humanoid = part.Parent:FindFirstChild("Humanoid") or part.Parent.Parent:FindFirstChild("Humanoid")
			local victim = game.Players:GetPlayerFromCharacter(part.Parent) or game.Players:GetPlayerFromCharacter(part.Parent.Parent)

			if victim and victim.TeamColor and victim.TeamColor == player.TeamColor then
				return
			elseif humanoid and humanoid.Parent ~= player.Character then
				UntagHumanoid(humanoid)
				TagHumanoid(humanoid, player)
				humanoid:TakeDamage(damage)
				local hitsound = handle.Hit:Clone()
				hitsound.Parent = part
				hitsound:Play()
			end
		end

You, however, have taken my attention into this section of the code. Is there something I did wrong here? Perhaps there is a way I could fix this “shooting at the sky breaks the script” issue through this section of the script? Or would the problem be related to the previous section? I appreciate any help and thank you for your reply.

Hi, when shooting a raycast at the sky and attempting to get an instance from the raycast, it wont work. You must check it. In the most recent code snippet you have sent, you must change if result then to if result and result.Instance then
I hope this helps you, let me know if this does not work.

1 Like

Unfortunately, it has not solved the issue, but I do understand the logic behind checking if there is an instance before running the code, therefore I will leave that in the code. I’ll keep messing around with the code for now, although I have little hope in fixing this on my own. Thank you for the insight, however.

Hmm, if that has not fixed it, it must be something else in your code which stops it. Could you send some more code or look through each part to try to find which part crashes it.

1 Like

Sure. Here is the entire block of code that handles the event where the bullet raycast is created.

local on = true

shootevent.OnServerEvent:Connect(function(player, position)
	if on and player.Character:FindFirstChildOfClass("Humanoid").Health>=1 then
		on = false
		local origin = gun.Handle.Position
		local direction = (position - origin).Unit
		local result = workspace:Raycast(origin, direction)

		local intersection = result and result.Position or origin + direction * range
		local distance = (origin - intersection).Magnitude
		print(distance)

		local bullet_clone = ServerStorage.Bullet:Clone()
		bullet_clone.Size = Vector3.new(0.1, 0.1, distance)
		bullet_clone.CFrame = CFrame.new(origin, intersection)*CFrame.new(0, 0, -distance/2)
		bullet_clone.Parent = workspace

		local humanoid = gun.Parent:FindFirstChildOfClass("Humanoid")
		local anim = humanoid:LoadAnimation(script.Shoot):Play()

		local rand = Random.new()
		bang.Pitch = rand:NextInteger(95, 105) / 100
		bang:Play()

		if result and result.Instance then
			local part = result.Instance
			local humanoid = part.Parent:FindFirstChild("Humanoid") or part.Parent.Parent:FindFirstChild("Humanoid")
			local victim = game.Players:GetPlayerFromCharacter(part.Parent) or game.Players:GetPlayerFromCharacter(part.Parent.Parent)

			if victim and victim.TeamColor and victim.TeamColor == player.TeamColor then
				return
			elseif humanoid and humanoid.Parent ~= player.Character then
				UntagHumanoid(humanoid)
				TagHumanoid(humanoid, player)
				humanoid:TakeDamage(damage)
				local hitsound = handle.Hit:Clone()
				hitsound.Parent = part
				hitsound:Play()
			end
		end

		gun.Handle.PointLight.Enabled = true

		task.wait(0.05)

		gun.Handle.PointLight.Enabled = false

		if bullet_clone then
			bullet_clone:Destroy()
		end

		task.wait(firerate - 0.05)

		on = true
	end
end)

I don’t know where else the issue would be, but you seem knowledgeable. I hope this can be of use.

Hmm, if you were to shoot a wall and not a player, would it do the same thing?
And if you do shoot the player, does it do the damage and everything and then not the second time?

1 Like

Actually, right now, it seems that whenever I shoot anything that isn’t the sky, the weapon works, but the ray always has a distance equal to the maximum range, and it doesn’t seem to actually hit and stop at its target, be it a wall or an enemy, neither does it damage anything. Additionally, it seems to randomly stop working if I keep shooting it, and the sky issue remains. It’s weird.

Yeah that is quite weird. I don’t know how you could fully fix this code-wise, however you could just put a fully transparent part to stop it from hitting the sky. Sorry I could not fully fix the problem however I hope this helps and is a solution to your problem! :slight_smile:

1 Like

I thank you regardless. To be honest, my fear of no one showing up to help was bigger than my fear of my issue not being solved. It’s good to know at least someone cares, and I hope that at some point this problem can be solved.

It seems I have solved a few issues by undoing a change I had done, and setting the direction part of the raycast creation like this:
local direction = (position - origin).Unit*range
It has not, however, solved the sky issue, so I set the code to print out what exactly the raycast is hitting. It seems that whenever I shoot at the sky or directly under my character, the raycast hits my own character and that breaks the code. Therefore, I am now trying to filter out the player character from being hit by the raycast.

local raycastParams = RaycastParams.new()
raycastParams.FilterType = Enum.RaycastFilterType.Exclude
raycastParams.FilterDescendantsInstances = {player.Character}

It, however, is not working. The raycast still hits the player. How can I get this to work? I believe that when I manage to fix this, perhaps the entire issue will be gone.

At last, it is solved. All I had to do was add “raycastParams” to the result, as in:
local result = workspace:Raycast(origin, direction, raycastParams)
Now the issue does not happen anymore and the weapon is fixed. I guess I really could count on myself, though it took me a lot of searching and trial to get to this.

2 Likes

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