#Raycast Bug

1 messages · Page 1 of 1 (latest)

digital stirrup
#

Hello! I have this raycast system for my gun, the raycast lines are correct, but sometimes it doesn't do the if ray then

remote.OnServerEvent:Connect(function(player, action, mousePos)
    --code that works...

    local character = player.Character
    if not character then return end

    local head = character:FindFirstChild("Head")
    if not head then return end

    local origin = head.Position

    local direction = (mousePos - origin)
    local distance = direction.Magnitude
    if distance == 0 then return end
    local rayDirection = direction.Unit * math.min(distance, 300)
    
    if Debug then
        local debugRay = Instance.new("Part")
        debugRay.Name = "DebugRay"
        debugRay.Anchored = true
        debugRay.CanCollide = false
        debugRay.CanTouch = false
        debugRay.CanQuery = false
        debugRay.Material = Enum.Material.Neon
        debugRay.Color = Color3.new(1, 0, 0)

        local rayLength = rayDirection.Magnitude

        debugRay.Size = Vector3.new(0.1, 0.1, rayLength)
        debugRay.CFrame = CFrame.new(origin, origin + rayDirection) * CFrame.new(0, 0, -rayLength/2)

        debugRay.Parent = workspace
    end

    local character = player.Character
    if not character then return end

    local blacklist = {GunHandle, GunHandle.Slide, GunHandle.Shell, GunHandle.Muzzle}
    
    for _, part in pairs(character:GetDescendants()) do
        if part:IsA("BasePart") then
            part.CanQuery = false
        end
    end

    for i, v in pairs (player.Character:GetChildren()) do
        if v:IsA("Accessory") then
            local hnd = v:FindFirstChild("Handle")
            if hnd then
                hnd.CanQuery = false
            end
        end
    end

    local params = RaycastParams.new()
    params.FilterDescendantsInstances = blacklist
    params.FilterType = Enum.RaycastFilterType.Blacklist
    params.IgnoreWater = true

    local ray = workspace:Raycast(origin, rayDirection)

    if ray then --here is the problem
        print(ray.Instance)
        print(ray.Position)
        
        local part = Instance.new("Part")
        part.Name = "ShootedPosPart"
        part.Parent = workspace
        part.Anchored = true
        part.CanCollide = false
        part.CanTouch = false
        part.Size = Vector3.new(0.2, 0.2, 0.2)
        part.Color = Color3.new(0, 0, 0)
        part.Position = ray.Position

        game.Debris:AddItem(part, 5)

        local sound = ConcreteImpactSound:Clone()
        sound.Parent = part
        sound:Play()

        local effect = game:GetService("ReplicatedStorage"):FindFirstChild("VFX"):FindFirstChild("Bullet"):Clone()
        effect.Parent = part
        effect.Enabled = true
        task.wait(0.15)
        effect.Enabled = false
    end
    
    task.wait(cooldownTime)
    
    cooldown = false
end)
snow bridge
#

Instead of looping through parts to change CanQuery, add the entire character and the gun to the FilterDescendantsInstances list. It’ll be more reliable and likely is whats causing ur bug.

mild plover
paper wyvern
#

try filtering the whole player character

snow bridge
digital stirrup
mental furnaceBOT
#

studio** You are now Level 1! **studio

digital stirrup
snow bridge
#

then the only other thing it could be is the ray is hitting something else and you think it's not firing again

mental furnaceBOT
#

studio** You are now Level 6! **studio

snow bridge
#

I would imagine the inside of the players head or something

#

in the code you have parims but you didn't actually pass them to the raycast function anywhere

#

so it is likely hitting whatever the ray is coming from at the right angle/position

digital stirrup
#

it doesn't do it

mild plover
#

just to see what it prints

digital stirrup
# mild plover try printing the ray before checking if ray then

16:20:26.227 16 - Servidor - ServerCore:104
16:20:26.550 15 - Servidor - ServerCore:104
16:20:26.984 14 - Servidor - ServerCore:104
16:20:27.341 13 - Servidor - ServerCore:104
16:20:27.676 12 - Servidor - ServerCore:104
16:20:27.784 Part - Servidor - ServerCore:166
16:20:27.786 4.053806304931641, -4.76837158203125e-07, -12.319916725158691 - Servidor - ServerCore:167
16:20:28.233 11 - Servidor - ServerCore:104
16:20:28.750 10 - Servidor - ServerCore:104
16:20:28.859 Part - Servidor - ServerCore:166
16:20:28.860 3.1678831577301025, 1.5660672187805176, -14.343300819396973 - Servidor - ServerCore:167
16:20:29.300 9 - Servidor - ServerCore:104
16:20:29.641 8 - Servidor - ServerCore:104
16:20:29.742 Part - Servidor - ServerCore:166
16:20:29.743 4.48138427734375, 1.8726325035095215, -14.107338905334473 - Servidor - ServerCore:167
16:20:30.033 7 - Servidor - ServerCore:104
16:20:30.400 6 - Servidor - ServerCore:104

#

sorry if it's spanish

#

servidor = server

#

the numbers are the bullets

mild plover
#

on which bullet did it not work?

digital stirrup
mild plover
#

or on which print

mild plover
digital stirrup
#

let me see

digital stirrup
mild plover
digital stirrup
#

ok

mild plover
#

alr

#

now try if ray.Instance then

digital stirrup
digital stirrup
#

(I shooted all the shots to a part)

mild plover
#

Sorry I needed to do smth rq

#

Could you maybe record a video of you shooting the weapon and the output

digital stirrup
digital stirrup
mental furnaceBOT
#

studio** You are now Level 2! **studio

digital stirrup
#

oh, sorry. It's mkv, can I send it in mp4

#

wait a moment

mild plover
#

I found the problem

#

So basicly i copied your script and found out that your debug part that you create isn't Blacklisted or is neither set to CanQuery

#

or wait

#

huh

#

you did set it to CanQuery = false

digital stirrup
digital stirrup
mild plover
#

Atleast it doesn't detect the player anymore

#

I just made it so that the character is inside the Params. (And I got rid of everything i couldn't use ofc)

#

nvm

#

the second i test it again to record i legit get a bug 💔

#

Now it finds some

#

ShootedPosPart

#

ah

#

nvm it just got the torso and after that only shoot at the ShootedPosPart

#

This is just ragebait

digital stirrup
#

the ray connected

#

but it says this

mild plover
#

that is extremly weird. For me it only says so when i aim my ray into the sky

digital stirrup
#

the only bug it's that it doesn't make the part when it impacts

#

or at least I don't see it

#

@mild plover In ur script the params aren't in the ray local ray = workspace:Raycast(origin, rayDirection)

#

thats why the black part is in the head

mild plover
#

Bruh

digital stirrup
#

@mild plover I FIXED IT

#

YOOOOOOOOOOO

#

FINALLY

#

It was a stupid bug!

#

local rayDirection = direction.Unit * math.max(distance, 300)

#

in my code it's math.min

#

I just had to use max

#

thank u for the help

mild plover
#

Well atleast it is fixed now.

mild plover