Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Why doesn't my ray casting script work in game but in studio?

Asked by 5 years ago

I'm not really good at raycasting so I don't know how to do stuff. Really I wanted to use Filtering Enabled but I can't access my weapon with a server script due to it being in the camera unless I learn. Any help would be appreciated

function createRay()

            local muzzle = gun[gun.Name].Flash
    local ray = Ray.new(mouse.Hit.p,muzzle.CFrame.lookVector*999)
    local hit,pos = workspace:FindPartOnRayWithIgnoreList(ray,{char})

    if hit then
            local human = hit.Parent:FindFirstChild("Humanoid")
            if human then
            print(hit.Name)
            if human.Health >0 then
            player.PlayerGui.HUD.Crosshair.Hit:Play()
            player.PlayerGui.HUD.Crosshair.Frame.Hitmarker.Visible = true

            if hit.Parent == "Head" then
            human:TakeDamage(s.dmg * s.multi)

            elseif hit.Parent:IsA("Part") then
            human:TakeDamage(s.dmg * s.damage)

            elseif hit.Parent:FindFirstChild("Humanoid") then
            human:TakeDamage(s.dmg)


            end
            end
            end
            end
end

Sometimes the ray wouldn't detect the head even though I'm aiming straight at it. Not sure why? Even the Upper Torso. Maybe I should try R6 instead

1 answer

Log in to vote
0
Answered by
aazkao 787 Moderation Voter
5 years ago
Edited 5 years ago

The Direction of your ray constructor is wrong, using lookvector will only make the ray cast straight in front of your head, you need to get the direction of where the ray is supposed to cast

Im assuming your char is referencing the player character

local ray = Ray.new(mouse.Hit.p,(mouse.hit.p-char.Head.Position).unit*999)

Here i made a gun to show what will happen if you use lookvector: https://gyazo.com/cd5015c1afdf5437918c00101ce94eee

And if you constructed the ray correctly: https://gyazo.com/2f957a27cd63375072a9fb1aea9a7a45

Note that the beams are being blocked by other beams because i didnt add beams to the ignorelist

0
I can't test it due to roblox being down but thanks for the help! GameBoyOtaku 63 — 5y
0
No problem, any errors with your script feel free to come back and ask, so long as you attempted it aazkao 787 — 5y
0
of course! I always try things first GameBoyOtaku 63 — 5y
0
oh I almost forgot the raycast won't do damage in game though what do I have to do here? GameBoyOtaku 63 — 5y
View all comments (3 more)
0
change the health property of the humanoid, you already got the humanoid in the variable human aazkao 787 — 5y
0
lol, at your video you forgot to use the ":destroy" thing, so the bullet will just float arround for ever mewant_taco 17 — 5y
0
yes i did that intentionally for the sake of this demonstration aazkao 787 — 5y
Ad

Answer this question