#Raycast returning nil

1 messages · Page 1 of 1 (latest)

glacial prairie
#
--In a LocalScript:
local Tool = script.Parent

function CFrameToOrientation(cf: CFrame)
    local rx, ry, rz = cf:ToOrientation()
    return Vector3.new(math.deg(rx), math.deg(ry), math.deg(rz))
end

Tool.Activated:Connect(function()

    local RayOrigin = Tool.Handle.Position
    local Player = game.Players.LocalPlayer
    local RayTarget = Player:GetMouse().Hit.Position
    local RayDirection = CFrameToOrientation(CFrame.lookAt(RayOrigin, RayTarget))*math.huge
    
    game.ReplicatedStorage.FireRaycast:FireServer(RayOrigin, RayDirection)
end)
--In a ServerScript:
game.ReplicatedStorage.FireRaycast.OnServerEvent:Connect(function(Player, RayOrigin, RayDirection, Params)
    
    local Params = RaycastParams.new()
    Params.FilterDescendantsInstances = {Player.Character, script.Parent}
    Params.FilterType = Enum.RaycastFilterType.Exclude
    
    local RaycastResult = game.Workspace:Raycast(RayOrigin, RayDirection, Params)
    
    if RaycastResult then
        if RaycastResult.Instance.Parent:FindFirstChild("Humanoid") then
            RaycastResult.Instance.Parent.Humanoid:TakeDamage(10)
        end

        local Tracer = Instance.new("Part")
        Tracer.BrickColor = BrickColor.new("Bright yellow")
        Tracer.Transparency = 0.5
        Tracer.Position = RaycastResult.Position - RayOrigin
    else
        warn("No RaycastResult") --This is firing, no matter where i click
    end
end)
daring fossil
#

this looks like an invalid direction local RayDirection = CFrameToOrientation(CFrame.lookAt(RayOrigin, RayTarget))*math.huge

#
function CFrameToOrientation(cf: CFrame)
    local rx, ry, rz = cf:ToOrientation()
    return Vector3.new(math.deg(rx), math.deg(ry), math.deg(rz))
end```
this is a rotation and not a direction
glacial prairie
#

funny because my friend got me to make the exact same change and it didn't work

#

(dont mind the random displayname and avatar change its for a bit in a dif server)

daring fossil
glacial prairie
#

so why does it not work then?

daring fossil
#

use this as your directionCFrame.lookAt(RayOrigin, RayTarget).LookVector * 1000

#

instead of allat cframetoorientation * math.huge insanity

glacial prairie
#

i just used math.huge to represent it has infinite range

daring fossil
#

do not use math.huge in a raycast

glacial prairie
#

kk

#

why not??

daring fossil
#

invalid

#

pretty sure the internal range limit is 10k studs or something like that, a far cry from math.huge levels of range.