Need help making raycasts more reliable (EDIT: Formatted code better I think)

I’m trying to make a ray gun using raycasting but most of the time, the ray doesn’t fire and the RaycastResult returns nil. (I can see because I put code in to visualize the beam)
I’m using Mouse:Hit() to determine the direction.

Here’s my code, I don’t know if it would help

Client

local tool = script.Parent
local plr = game.Players.LocalPlayer
local char = plr.Character or plr.CharacterAdded:Wait()
local h = char:WaitForChild("Humanoid")
local animator = h:WaitForChild("Animator")

tool.Activated:Connect(function()

	if tool.Enabled == true then
		tool.Enabled = false

		local mouse = plr:GetMouse()
		local mousePos = mouse.Hit.Position
		print(mousePos)

		tool.Fire:FireServer(mousePos)
		wait(1.5)
		tool.Enabled = true
	end
end)

tool.Fire.OnClientEvent:Connect(function()
	local failAnim = animator:LoadAnimation(script.failAnim)
	tool.Parent = plr.Backpack
	failAnim:Play()
	wait(1)
	tool.Parent = char
	failAnim:Stop()
end)

Server

local tool = script.Parent
local tweenService = game:GetService("TweenService")

function openDoor(ray)
	local button = ray.Instance
			button.BrickColor = BrickColor.new("Bright green")

			local door = workspace.Doorway.Door

			local info = TweenInfo.new(
				3.5,
				Enum.EasingStyle.Linear,
				Enum.EasingDirection.In)
			
			local goals = {
			Position = Vector3.new(-25.96, 23.716, 60.5)}

			local tween = tweenService:Create(door,tweenInfo,goals)
			tween:Play()	
end

tool.Fire.OnServerEvent:Connect(function(plr,hit)

	local char = plr.Character
	local h = char.Humanoid
	local beam = script.RayBeam:Clone()
	local originAttach = tool.Handle.OriginAttachment:Clone()
	local ray = workspace:Raycast(originAttach.Position,hit) 

	if ray then

		print(ray.Instance.Name)
		tool.Handle.Fire:Play()
		local position = ray.Position

		local attach = Instance.new("Attachment")
		attach.Name = "HitAttachment"
		attach.Parent = workspace["Big ol Box"].Baseplate
		attach.Position = position
		originAttach.Parent = workspace["Big ol Box"].Baseplate
		beam.Parent = workspace.Beams
		beam.Attachment1 = attach
		beam.Attachment0 = tool.Handle.OriginAttachment

			if ray.Instance.Name == "Button" then
				openDoor(ray)
			end

		wait(1.5)
		beam:Destroy()
		attach:Destroy()
		originAttach:Destroy()
		tool.Handle.Reload:Play()

	else 
		print("Couldn't find where the ray hit :(")
		tool.Fire:FireClient(plr)
	end
end)



Is there anything I’m doing wrong? This is the first time I’m using raycasts, so I’m pretty new.

PLEASE indent your code properly. If that’s how you actually code, no wonder errors and mistakes arise so easily. Indenting your code makes it more clear, concise, easier to read and interpret for both you and the people trying to help.

1 Like

yeah, i need to clean it up

uhh, what’s the best way to indent it?

i’m still kinda new to coding

Press CTRL + A (CMD + A on Macs) to select the entire document, then press Alt + Shift+ F

Script tab > Format Selection > Format Document

i did what you said, and nothing happened

Might be a silly question, but did you do this while viewing the script?

yeah, the script is open
i also selected everything

Alternatively, while viewing the script, you can navigate to Script > Format Selection > Format Document

The result should be like this

before:

after:
image

that still didn’t do anything
i have no idea why

Very strange :thinking: Well, I’m out of ideas, I’m sorry :pensive:

that’s okay
i’ll try formatting manually

Alright, I’m back.

I (hopefully) formatted the code better.

Is it easier to read?


Here’s a video of the problem I’m having.

Shooting off into the distance sometimes work but when I try shooting somewhere closer, like the ground, or that button, it doesn’t work and my character does that shrugging animation.

The issue is that you are using mouse.hit.position as the direction of the raycast. You should instead subtract the hit.position from the origin of the ray to get the actual direction.

local ray = workspace:Raycast(originAttach.Position,(hit - originAttach.Position).Unit * Range) 

with the range being the maximum range you want

1 Like

Thank you so much!
It worked! :grin: