Raycast Distance Issue

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    I am trying to make a tomb of the mask style game, and am using raycasting to find the distance between the player and the wall.

  2. What is the issue? Include screenshots / videos if possible!
    For some reason, the output is saying that it attempted to index nil with Distance, even though raycast should always return a distance value.

  3. What solutions have you tried so far? Did you look for solutions on the Creator Hub?
    I looked on the devforum, tried other solutions, nothing worked.

After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!

repeat task.wait() until script.Parent:IsA("Model")

local controlmodule = require(game.Players.LocalPlayer.PlayerScripts:WaitForChild("PlayerModule").ControlModule)
local UserInputService = game:GetService("UserInputService")
local tweenService = game:GetService("TweenService")
local plr = game.Players.LocalPlayer
local tweenInfo = TweenInfo.new(0.1, Enum.EasingStyle.Quad, Enum.EasingDirection.Out)
local char = script.Parent

controlmodule:Disable()

UserInputService.InputBegan:Connect(function(input, gpe)
	if input.KeyCode == Enum.KeyCode.W or input.KeyCode == Enum.KeyCode.Up then
		if gpe then return end

		local BV = Instance.new("BodyVelocity", char:WaitForChild("Torso"))
		local RayCastNew = workspace:Raycast(script.Parent:FindFirstChild("Torso").Position, script.Parent:FindFirstChild("Torso").CFrame.LookVector)
		BV.P = 3000
		BV.MaxForce = Vector3.new(math.huge,math.huge,math.huge)
		BV.Velocity = RayCastNew.Distance

		game.Debris:AddItem(BV, 0.1)
	end
end)

The workspace:Raycast() method returns nil if it didn’t hit anything. You’ll want to first check that RayCastNew is not nil before accessing its properties.

yeah, that was the problem, but the thing is im casting from the players torso, and this is the room:


(left to right, and the player is facing the right way)

You’re only casting the ray 1 stud from its origin (the player’s torso) because youre passing in a unit vector as the second argument in the raycast call. Multiply this unit vector by the distance you would like to cast the ray. For example, if you want to cast the ray 64 studs:

local origin = script.Parent:FindFirstChild("Torso").Position
local direction = 64 * script.Parent:FindFirstChild("Torso").CFrame.LookVector
local RayCastNew = workspace:Raycast(origin, direction)

ohh, i understand now. thanks for the help :slight_smile:

so i reworked the script a lil as it was flinging the player on impact, but now i’ve found that it’s raycasting the wrong direction, or maybe the tween is going the wrong way (i dont really know)

new script

repeat task.wait() until script.Parent:IsA("Model")

local controlmodule = require(game.Players.LocalPlayer.PlayerScripts:WaitForChild("PlayerModule").ControlModule)
local UserInputService = game:GetService("UserInputService")
local tweenService = game:GetService("TweenService")
local plr = game.Players.LocalPlayer
local tweenInfo = TweenInfo.new(0.1, Enum.EasingStyle.Quad, Enum.EasingDirection.Out)
local char = script.Parent

controlmodule:Disable()

UserInputService.InputBegan:Connect(function(input, gpe)
	if input.KeyCode == Enum.KeyCode.W or input.KeyCode == Enum.KeyCode.Up then
		if gpe then return end

		local RayCastNew = workspace:Raycast(script.Parent:FindFirstChild("Torso").Position, 64 * script.Parent:FindFirstChild("Torso").CFrame.LookVector)
		local RaycastStartBox = Instance.new("Part", workspace)
		RaycastStartBox.Position = script.Parent:FindFirstChild("Torso").Position
		RaycastStartBox.Orientation = script.Parent:FindFirstChild("Torso").Orientation
		RaycastStartBox.Size = script.Parent:FindFirstChild("Torso").Size
		RaycastStartBox.Anchored = true
		RaycastStartBox.Transparency = 0.5
		RaycastStartBox.Color = Color3.new(255, 0, 0)
		RaycastStartBox.CanCollide = false
		if RayCastNew == nil then return end
		
		tweenService:Create(script.Parent.Torso, tweenInfo, {Position = Vector3.new(script.Parent.HumanoidRootPart.Position.X + RayCastNew.Distance, script.Parent.HumanoidRootPart.Position.Y,  script.Parent.HumanoidRootPart.Position.Z)}):Play()
		
	end
end)
1 Like