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

Why does the attacking Player taking damage from their own weapon/gun?

Asked by 2 years ago

Hello there! I was wondering why your own gun does damage to you instead of your target, like a dummy for example. This is really something that I don't know how to script, so I would appreciate it if I got some help; Thanks! More Info below!

-- Variables
local TweenService = game:GetService("TweenService")
local Tweeninfo = TweenInfo.new(0.3, Enum.EasingStyle.Linear, Enum.EasingDirection.Out, 0, false, 0)
-- Scripting in Action
script.Parent.ShootEvent.OnServerEvent:Connect(function(player, caster, position)
    local rayOrigin = caster.Position
    local rayDirection = position

    local rayParams = RaycastParams.new()
    rayParams.FilterDescendantsInstances = {player.Character}
    rayParams.FilterType = Enum.RaycastFilterType.Blacklist

    local rayResult = game.Workspace:Raycast(rayOrigin, (position - script.Parent.Handle.Position) * 200, rayParams)

    local hittedPart = rayResult.Instance

    local bullet = Instance.new("Part", game.Workspace)
    bullet.Size = Vector3.new(0.25, 0.25, 0.25)
    bullet.Shape = Enum.PartType.Cylinder
    bullet.Position = script.Parent.PistolModel.Flash.Position
    bullet.BrickColor = BrickColor.new("Gold")

    local TweenTo = {CFrame = CFrame.new(position)}

    local TweenBullet = TweenService:Create(bullet, Tweeninfo, TweenTo)

    TweenBullet:Play()

    if hittedPart.Parent:IsA("Model") then
        bullet.Touched:Connect(function(hit)
            local charcter = hit.Parent

            local humanoid = charcter:FindFirstChild("Humanoid")

            if humanoid then
                bullet:Destroy()
                if hittedPart.Name == "Head" then
                    humanoid:TakeDamage(25)
                    print("Headshot Acquired!")
                else
                    humanoid:TakeDamage(16)
                    print("Shot!")
                end
            else
                wait(5)
                bullet:Destroy()
            end
        end)
    end

end)
0
I don't really use tweenservice but I made a gun thats easy to script, It may not be based off the problem you have having but can I show It to you? imnotaguest1121 362 — 2y

2 answers

Log in to vote
0
Answered by 2 years ago
Edited 2 years ago

The problem it's that inside the Raycast params, you should put instead of the model, the childrens, or atleast the ones that can get hitted by the ray using :GetChildren(). I'd recommend making a table and putting inside all the BaseParts and Accessories of the player. Also, where does the variable position come from?

Ad
Log in to vote
0
Answered by 2 years ago
Edited 2 years ago

Edit: I marked your answer correct, I've used another source of information to help out with my scripting problem, a DevForum post, but anyway thanks for trying to help! I really appreciate it!

Answer this question