Ad
Log in to vote
0

How can I modify my gun script so It shoots where the player Is facing?

Asked by 13 days ago

Hello Developers!

As the title says, below this text Is a script, the script shoots out a bullet which deals damage on contact, the problem however Is that I want to learn how to make bullets shoot where the player Is facing

local Tool = script.Parent


Tool.RemoteEvent.OnServerEvent:Connect(function()
    local Ball = Instance.new("Part",game.Workspace)
    local CanDamage = true
    game:GetService("Debris"):AddItem(Ball,3)
    Ball.Position = Tool.Handle.Position
    Ball.Orientation = Tool.Parent.Torso.Orientation
    Ball.Size = Vector3.new(1.02, 1.02, 1.02)
    Ball.CastShadow = false
    Ball.Anchored = false
    Ball.Material = Enum.Material.Grass
    Ball.Shape = Enum.PartType.Ball
    Tool.Handle.Fire:Play()
    Tool.Handle.Attachment.Fire.Enabled = true
    Tool.Handle.Attachment.PointLight.Enabled = true
    Ball.Touched:Connect(function(hit)
        local Player = game:GetService("Players"):GetPlayerFromCharacter(hit.Parent)
        if Player then
            local player1 = game.Players:GetPlayerFromCharacter(hit.Parent)
            local player2 = game.Players:GetPlayerFromCharacter(Tool.Parent)

            if (player1 ~= nil) and (player2 ~= nil) then
                local humanoid = (player1.Character or player1.CharacterAdded:Wait()):FindFirstChildWhichIsA("Humanoid")

                if (player1.TeamColor ~= player2.TeamColor) then
                    if (humanoid ~= nil) and CanDamage == true then
                        humanoid:TakeDamage(15)
                        CanDamage = false
                    end
                end
            end
        elseif not Player then
            local humanoid = hit.Parent:WaitForChild("Humanoid")
            if humanoid and CanDamage == true then
                humanoid:TakeDamage(15)
                CanDamage = false
            end
        end
    end)
    local KnockBack = Instance.new("VectorForce")
    local Attachment = Instance.new("Attachment")

    KnockBack.Parent = Ball
    Attachment.Parent = Ball
    KnockBack.Attachment0 = Attachment
    KnockBack.ApplyAtCenterOfMass = true
    KnockBack.Force =  Vector3.new(0,300,-6000)
    game:GetService("Debris"):AddItem(KnockBack,.05)
    game:GetService("Debris"):AddItem(Attachment,.05)
    wait(0.1)
    Tool.Handle.Attachment.Fire.Enabled = false
    Tool.Handle.Attachment.PointLight.Enabled = false
end)

1 answer

Log in to vote
1
Answered by 12 days ago

You can find the direction an object is facing using LookVector.

local DirectionPlayerIsFacing = game.Players.LocalPlayer.Character.HumanoidRootPart.CFrame.LookVector

This would give a vector3 value that is 1 stud in front of the player, relative to the player.

Ad

Answer this question