Player takes damage when using their own weapon

Hey everyone! I have been working on a gun and came across the player taking damage when they were moving and shooting. I have tried to add a script that makes the bullet not do damage for 1 second, but I’m not the best scripter. If you have any info on how to fix this, please let me know. Thanks!
Video

If you can could you show the gun’s code?

For the gun or bullet? I can do both

Most preferably the bullet, but do both please.

2 Likes

Since you’re probably using Raycasting, add the Player’s character to the raycast whitelist.

Or, you could use :GetPlayerFromCharacter(Player) and then get their ID, and compare it to the Player’s ID.

I would recommend using the first option.

1 Like

Please copy and paste the code instead of taking a screenshot as it’s much more readable.

script.Parent.Shoot.OnServerEvent:Connect(function(player,LookAt)
local Gun = player.Backpack:FindFirstChild(“Gun”) or player.Character:FindFirstChild(“Lazer Pistol”)–rename gun to the tool name
local cframe = Gun.Barrel.CFrame
local bullet = game.ReplicatedStorage.Bullet:Clone()–rename bullet to the name of your bullet if it is different
bullet.Parent = script.Parent
bullet.CFrame = cframe
bullet.Velocity = LookAt*100 --change this number to what you want the speed of your bullet to go at
local fly = Instance.new(“BodyForce”,bullet)
fly.Force = Vector3.new(0,bullet:GetMass()*196.2,0)
game.Debris:AddItem(bullet,10)
wait()
bullet.Touched:Connect(function()
wait(0.25)
bullet:remove()
end)
end)


script.Parent.Activated:Connect(function()

local pos = script.Parent.Parent.Humanoid.TargetPoint

local LookAt = (pos - script.Parent.Parent.Head.Position).Unit

script.Parent.Shoot:FireServer(LookAt)

end)


local debounce = true
local timebetdamage = 1
script.Parent.Touched:Connect(function(hit)

if hit.Parent:FindFirstChild(“Humanoid”) then
if debounce == true then
debounce = false
hit.Parent.Humanoid:TakeDamage(20)
–change this number here to the amount of damage you’d like your bullet to do
wait(timebetdamage)
debounce = true
end
end

end)

Hmm…
I wouldn’t recommend using a Force on a bullet as that’s very unreliable and it would be difficult to define the shooting player from the bullet without a RemoteEvent (along with VERY inaccurate aiming).

Instead, check this tutorial out.

local debounce = true
local timebetdamage = 1
script.Parent.Touched:Connect(function(hit)
    if hit.Parent:FindFirstChild(“Humanoid”) then
            if debounce == true then
            debounce = false
            hit.Parent.Humanoid:TakeDamage(20) –change this number here to the amount of damage you’d like your bullet to do
            wait(timebetdamage)
            debounce = true
        end
    end
end)
script.Parent.Activated:Connect(function()
    local pos = script.Parent.Parent.Humanoid.TargetPoint
    local LookAt = (pos - script.Parent.Parent.Head.Position).Unit
    script.Parent.Shoot:FireServer(LookAt)
end)
local debounce = true
local timebetdamage = 1
script.Parent.Touched:Connect(function(hit)
    if hit.Parent:FindFirstChild(“Humanoid”) then
    if debounce == true then
        debounce = false
        hit.Parent.Humanoid:TakeDamage(20) –change this number here to the amount of damage you’d like your bullet to do
        wait(timebetdamage)
        debounce = true
        end
    end
end)

Formatted the scripts for the sake of my eyes and brain

2 Likes

Does look promising, but it’s gonna mess up with the rest of the code. I’ll still try it

local debounce = true
local timebetdamage = 1
script.Parent.Touched:Connect(function(hit)
   if hit.Parent ~= script.Parent.Parent then --try this
 if hit.Parent:FindFirstChild(“Humanoid”) then
    if debounce == true then
        debounce = false
        hit.Parent.Humanoid:TakeDamage(20) –change this number here to the amount of damage you’d like your bullet to do
        wait(timebetdamage)
        debounce = true
        end
    end
  end
end)
1 Like

@MediaHQ @BonesXVII and @J4Y_JP , thanks for all helping me with this issue! I’ll definitely start looking into raycasting Bones!

1 Like

No problem! Glad I could help. Don’t be afraid to send me a PM if you have any further issues regarding this (or any) topics.

1 Like

With enough editing

RocketPropulsion | Roblox Creator Documentation should work.

Simply make a new part, set the transperency to 1 and cancollide off + cantouch off
Set the projectile’s cframe to the parts cframe
then set the velocitys and etc

1 Like