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

My Raycast somehome avoids the player? How can I fix this?

Asked by 7 years ago
Edited by M39a9am3R 6 years ago

Whenever I try to shoot at an object, it someone avoids it, otherwise it's fine. Here's the code.

Player = game:GetService("Players").LocalPlayer
Mouse = Player:GetMouse()
Gun = script.Parent

Equip = false



Gun.Unequipped:connect(function()
    Equip = false
    print("Not using pistol")
end)

Gun.Equipped:connect(function()
    Mouse.Icon = "http://www.roblox.com/asset/?id=79658449"
    Equip = true
    print("Using pistols")


end)




Gun.Activated:connect(function()
    if Equip == true then

        --Raycasting
        local BulletRayMatrix = Ray.new(Gun.Handle.CFrame.p, (Mouse.Hit.p).unit*500)
        local part, position = workspace:FindPartOnRay(BulletRayMatrix, Player.Character, false, true) --Part, object that was hit

        local Bullet = Instance.new("Part", workspace)
        Bullet.BrickColor = BrickColor.new("Really yellow")
        Bullet.Material = "Neon"
        Bullet.Transparency = 0.5
        Bullet.Anchored = true
        Bullet.Locked = true
        Bullet.CanCollide = false



        local Distance = (Gun.Handle.CFrame.p - position).Magnitude
        Bullet.Size = Vector3.new(0.3, 0.3, Distance)
        Bullet.CFrame = CFrame.new(Gun.Handle.CFrame.p, position) * CFrame.new(0, 0, -Distance / 2)

        --Damage
        Bullet.Touched:connect(function(hit)
            local hum = hit.Parent:FindFirstChild("Humanoid")
            if hum ~= nil and hit.Parent.Name ~= Player.Name then

            end
        end)
        --
        game.Debris:AddItem(Bullet, 0.2)


        --Raycasting
        print("shooting")
    else
        return;

    end
end)

Any soloutions for this? It's like when I shoot at it it shoots over the object I want to shoot at :(!

1 answer

Log in to vote
0
Answered by
RubenKan 3615 Moderation Voter Administrator Community Moderator
7 years ago

For the player it could be because mouse has a targetFilter property wich is default set to the character. try adding mouse.TargetFilter = nil and see if that fixes it for the player part.

There's not enough information to give a decent/broad answer

0
When firing at nothing, sky or just infront of nothing it shoots normal and casts to where I want it. But when I fire at things that has humanoids it basically avoids it, it shoots above or in the side of it. LightModed 81 — 7y
Ad

Answer this question