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

How to make raycast go through player who casted it?

Asked by 1 year ago
Edited 1 year ago

In my game players can use a raycast gun, but when shooting the gun to an area behind them, the ray hits the caster, I've come up with a temporary solution that makes it so the ray doesn't deal damage to them, but they still end up blocking their own shot. Is there any way to make it so the ray will pass through the user that fired the gun?

local tool = script.Parent
local shoot_part = tool:WaitForChild("Barrel")
local remote = tool:WaitForChild("Fire")
local user = script.Parent.Parent.Parent
local user2 = user.Character.Humanoid
local user3 = user.Character


local Workspace = game:GetService("Workspace")
local ServerStorage = game:GetService("ServerStorage")
local cd = false
remote.OnServerEvent:Connect(function(player, position)
    if not cd then
        cd = true
        spawn(function()
            wait(3)
            cd = false
        end)
    local origin = shoot_part.Position
    local direction = (position - origin).Unit*300
    local result = Workspace:Raycast(origin, direction)

    local intersection = result and result.Position or origin + direction
    local distance = (origin - intersection).Magnitude
    local light = script.Parent.Handle.BlamLight
    local fire = script.Parent.Handle.BlamFire
    local shoot = script.Shoot
    local nuts = user2:LoadAnimation(shoot)
    nuts:Play()
    spawn(function()
        light.Enabled = true
        fire.Enabled = true
        wait(0.1)
        fire.Enabled = false
        light.Enabled = false
    end)
    local gun = script.Parent.Handle.Fire:Clone()
    gun.Parent = user3.HumanoidRootPart
    gun:Play()
    spawn(function()
        wait(4)
        gun:Destroy()
    end)
    local bullet_clone = ServerStorage.Bullet:Clone()
    bullet_clone.Size = Vector3.new(0.1, 0.1, distance)
    bullet_clone.CFrame = CFrame.new(origin, intersection)*CFrame.new(0, 0, -distance/2)
    bullet_clone.Parent = Workspace
    local random = Random.new()


    if result then
        local part = result.Instance
        local humanoid = part.Parent:FindFirstChild("Humanoid") or part.Parent.Parent:FindFirstChild("Humanoid")
        local torso = part.Parent:FindFirstChild("Torso") or part.Parent.Parent:FindFirstChild("Torso")
        local root = part.Parent:FindFirstChild("HumanoidRootPart") or part.Parent.Parent:FindFirstChild("HumanoidRootPart")
        local charactervalues = part.Parent:FindFirstChild("charactervalues") or part.Parent.Parent:FindFirstChild("charactervalues")
            local material = part.Material

            if part.Name == "Part" then
                print("hit a part")
            end

        if humanoid then
            if charactervalues:WaitForChild("Role").Value ~= "Vampire" then 
                humanoid:TakeDamage(0)
            else
                humanoid:TakeDamage(60)
                local sound = game.ServerStorage.Hit:Clone()
                sound.Parent = root
                sound.PlaybackSpeed =   random:NextNumber(0.5, 1.5)
                sound:Play()
                spawn(function()
                    wait(3)
                    sound:Destroy()
                end)
                end
            end

        end




    wait(0.25)
        bullet_clone:Destroy()
        end
end)

1 answer

Log in to vote
1
Answered by 1 year ago

While using Workspace:Raycast, utilize the third parameter to add a filter on the character to ignore it.

0
im real new to raycasting so can you tell me how i can do that :p Uniplier 83 — 1y
0
local raycastparams = {}; raycastparams.FilterDescendantsInstances = {character}; raycastparams,FilterType = Enum.RaycastFilterType.Whitelist;; Workspace:Raycast(origin,direction,raycastparams) BulletproofVast 1033 — 1y
0
Thanks for explaining, I had known a little about raycastparams but didnt know you had to put it in the 3rd parameter. Uniplier 83 — 1y
Ad

Answer this question