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

Ray Not Firing In The Correct Direction?

alright so as the title says i am making a ray from a basic script i just fire the ray and make a part on the ray so thats extremely simple no problem but the ray insists on just going in a single direction no matter how i change up the code or not at first i was think the to argument was the problem because it is coming from the players head as intended but i changed it up multiple times and still the same result and even sometimes it would fire same orientation but about 20 meters from the player

ModuleScript:

local Player = game.Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local debugline = {}
	function debugline.DrawDebugLine(From,To,Object)
		local ray = Ray.new(From,To)
		print(game.Workspace:FindPartOnRay(ray))
		local part, position = workspace:FindPartOnRay(ray, Character, false, true)
		local beam = Instance.new("Part")
		beam.Parent = workspace
		beam.BrickColor = BrickColor.new("Bright red")
		beam.FormFactor = "Custom"
		beam.Material = "Neon"
		beam.Transparency = 0.25
		beam.Anchored = true
		beam.Locked = true
		beam.CanCollide = false
		local distance = (Object.CFrame.Position - position).magnitude
		beam.Size = Vector3.new(0.3, 0.3, distance)
		beam.CFrame = CFrame.new(Object.CFrame.Position, position) * CFrame.new(0, 0, -distance / 2)
		game:GetService("Debris"):AddItem(beam, 3)
	end
return debugline

if you need the information on where the to and from arguments are coming from then i have it here:

LocalScript:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local RunService = game:GetService("RunService")
local DebugLine = require(ReplicatedStorage.LocalDebugTools:WaitForChild("DrawDebugLine"))
local Tool = script.Parent
local Player = game.Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local Head = Character:WaitForChild("Head")
local HumanoidRootPart = Character:WaitForChild("HumanoidRootPart")
local Mouse = Player:GetMouse()
local Door = ReplicatedStorage:FindFirstChild("ShackDoor")
local PlayerGui = Player:WaitForChild("PlayerGui")
local LookAtCamera = PlayerGui:WaitForChild("LookAtCamera")
local SoundEffect = PlayerGui:WaitForChild("SoundEffect")
local Spot = Instance.new("Vector3Value")
local Twist = Instance.new("Vector3Value")
local debounce = false
local raybounce = false
Tool.Equipped:Connect(function()
	local DoorClone = Door:Clone()
	DoorClone.Parent = workspace
	DoorClone.Name = "DoorBluePrint"
	LookAtCamera.Disabled = false
	wait(.3)
	while wait(.02) do
		if workspace:FindFirstChild("DoorBluePrint") then
			local loc = Head.Position + (Head.CFrame.lookVector * 12)
			local x = loc.x
			local y = loc.y
			local z = loc.z
			DoorClone:SetPrimaryPartCFrame(CFrame.new(x,y,z))
			DoorClone:SetPrimaryPartCFrame(lookAt(Head.Position, DoorClone.ShackDoor.CFrame.Position))
			lookAt(Head.Position,DoorClone.ShackDoor.CFrame.lookVector)
			Spot.Value = DoorClone.ShackDoor.Position
			Twist.Value = DoorClone.ShackDoor.Orientation
			if raybounce == false then
				raybounce = true
				DebugLine.DrawDebugLine(Head.Position,Head.Position + (Head.CFrame.lookVector * 12),Head) -- like i said i tried changing the second argument to many different things that should result in the same direction but no luck whatsoever
			end
		end
	end
--	DebugLine.DrawDebugLine(Head.Position,Spot.Value,Head)
end)

Tool.Unequipped:Connect(function()
	workspace.DoorBluePrint:Destroy()
	raybounce = false
	LookAtCamera.Disabled = true
end)

Tool.Activated:Connect(function()
    if Mouse.Target.Name == "DoorPosition" then
	local DoorPosition = Mouse.Target.Position
    local posX, posY, posZ = DoorPosition.X, DoorPosition.Y, DoorPosition.Z
    ReplicatedStorage.PlaceFabrication:FireServer("Door",posX, posY, posZ)
	--Tool:Destroy()
	--workspace.DoorBluePrint:Destroy()
	--LookAtCamera.Disabled = true
else
	end
end)

function lookAt(target, eye)
    local forwardVector = (eye - target).Unit
    local upVector = Vector3.new(0, 1, 0)
    local rightVector = forwardVector:Cross(upVector)
    local upVector2 = rightVector:Cross(forwardVector)
    return CFrame.fromMatrix(eye, rightVector, upVector2)
end

1 answer

Found the problem. You're making the ray incorrectly. Replace with this.

DebugLine.DrawDebugLine(Head.Position,Head.CFrame.lookVector.Unit * 12,Head)

The 2nd argument of Ray.new has to be a direction with a magnitude of however long you want the ray to be, and it's not supposed to be some point in the middle of nowhere (you were adding head.position which messed it up)

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