#How to script it so when a bullet hits a player, it does damage

131 messages · Page 1 of 1 (latest)

summer cosmos
#

I have a gun script that shoots bullets like a simple gun game, but I don't know how to make it so it does damage to players when the bullet hits the player. Could someone help me for how I could do this

#
local RemoteFunction = ReplicatedStorage:WaitForChild("RemoteFunction")
local Bullet = ReplicatedStorage:WaitForChild("Bullet")

local tool = script.Parent

local bulletSpeed = 200
local bulletDamage = 15
local fireRate = 0.1


local shooting = false
tool.Activated:Connect(function()
    if shooting == false then
        shooting = true
        
        local player = game.Players:GetPlayerFromCharacter(tool.Parent)
        local MouseHitLookVector = RemoteFunction:InvokeClient(player)
        
        local newBullet = Bullet:Clone()
        newBullet.CFrame = tool.Handle.CFrame
        newBullet.Parent = game.Workspace
        
        newBullet.Velocity = MouseHitLookVector * bulletSpeed
        
        
        wait(fireRate)
        shooting = false
    end
end)

print("Working")```
#

This is a normal script

#
local RemoteFunction = ReplicatedStorage:WaitForChild("RemoteFunction")

local player = game.Players.LocalPlayer
local mouse = player:GetMouse()

RemoteFunction.OnClientInvoke = function()
    return mouse.Hit.LookVector
end```
#

This is a local script

#

Both in the gun

#

got it from a tutorial and edited it to what I wanted to do

#

These are the parts of it

barren flame
#

Firstly, it does not show that it does damage. It only is that the bullet is shot, never stopped. And travels.

Your first error is newBullet.Velocity = MouseHitLookVector * bulletSpeed Where you try to multiply them together. MouseHisLookVector is not a number, so it will not work. Your bullet will not travel at all.

Your second error (if there is no damagescript) is that you do not cause any damage, so nothing happens at all.

summer cosmos
#

It does work tho

barren flame
summer cosmos
#

I can shoot it and it shoots the bullet

barren flame
summer cosmos
#

The damage was suppost to be 15 but fromt he tutorial I got, he changed it to 40 on another script

#

It shoots the bullet and then when it hits the ground it bounces since its going fast and then stays

barren flame
#

Oh, I was only going off of the previous scripts

#

So you say it does not damage the player? The only problem?

#

Can you show me the script?

summer cosmos
#

Well i haven't made it so it can't do damage cause im not sure how

#
local RemoteFunction = ReplicatedStorage:WaitForChild("RemoteFunction")
local Bullet = ReplicatedStorage:WaitForChild("Bullet")

local tool = script.Parent

local bulletSpeed = 400
local bulletDamage = 15
local fireRate = 0.1


local shooting = false
tool.Activated:Connect(function()
    if shooting == false then
        shooting = true
        
        local player = game.Players:GetPlayerFromCharacter(tool.Parent)
        local MouseHitLookVector = RemoteFunction:InvokeClient(player)
        
        local newBullet = Bullet:Clone()
        newBullet.CFrame = tool.Handle.CFrame
        newBullet.Parent = game.Workspace
        
        newBullet.Velocity = MouseHitLookVector * bulletSpeed
        
        
        wait(fireRate)
        shooting = false
    end
end)

Bullet.Touched:Connect(function(hit)
    if hit.Parent then
        local humanoid = hit.Parent:FindFirstChild("Humanoid")
        if humanoid then
            humanoid.Health = humanoid.Health - bulletDamage
            end
        end
    end
end)

print("Working")```
#

I just added the bullet.touched:connect part

#

just to see what could happen

barren flame
#

You did not disconnect it after also, something to look for

#

If the bullet you intend to make stays on the ground

#

Then someone steps on it, it will still harm them

#
BulletTouch = Bullet.Touched:Connect(function(hit)
    if hit.Parent then
        local humanoid = hit.Parent:FindFirstChild("Humanoid")
        if humanoid then
            humanoid.Health = humanoid.Health - bulletDamage
            end
        end
    end
  BulletTouch:Disconnect() // Just to make sure you have it not harm players after it its the ground, or another
end)```
summer cosmos
#

ok i'll try that. im not that great as scripting so there lots of errors in my script

#

rn it gave me an error at this part

#

it says it expected a ) to close line 33 got bullet touch

#

thats second line

#

ty for all the help btw

barren flame
#

Oh whoops

#

Move the BulletTouch:Disconnect outside the function

summer cosmos
#

ok one sec

barren flame
#

Above the print function

summer cosmos
#

it works but how should i test if it can do damage?

barren flame
#

Hmm, you would usually test it on a rig which could take damage

#

It should work exactly the same for a player

summer cosmos
#

when i shoot it at the rig, it goes through it and does nothing

#

should it be like one with health or does the roblox rig work fine

#

im using the roblox one

#

the premade rigs

barren flame
#

Ok wait let me think

summer cosmos
#

ok

barren flame
#

You never set the velocity to 0 when it is hit

#
BulletTouch = Bullet.Touched:Connect(function(hit)
  Bullet.velocity = 0 ------------------- Here
    if hit.Parent then
        local humanoid = hit.Parent:FindFirstChild("Humanoid")
        if humanoid then
            humanoid.Health = humanoid.Health - bulletDamage
            end
        end
    end```
summer cosmos
#

ok i'll try it

barren flame
#

But i'm not sure if it will work

summer cosmos
#

sadly didn't work

barren flame
#

Like i'm not exactly sure, because the way you defined the bullet in the script seems a bit odd

#

Like you have the newbullet in the shoot function, right?

#

But we of course cannot access it outside

summer cosmos
#

yes I think

barren flame
#

So, we have to move it inside the shooter function

summer cosmos
#
tool.Activated:Connect(function()
    if shooting == false then
        shooting = true
        
        local player = game.Players:GetPlayerFromCharacter(tool.Parent)
        local MouseHitLookVector = RemoteFunction:InvokeClient(player)
        
        local newBullet = Bullet:Clone()
        newBullet.CFrame = tool.Handle.CFrame
        newBullet.Parent = game.Workspace
        
        newBullet.Velocity = MouseHitLookVector * bulletSpeed
        
        
        wait(fireRate)
        shooting = false
    end
end)```
barren flame
#

Yes, that one

#
tool.Activated:Connect(function()
    if shooting == false then
        shooting = true
        
        local player = game.Players:GetPlayerFromCharacter(tool.Parent)
        local MouseHitLookVector = RemoteFunction:InvokeClient(player)
        
        local newBullet = Bullet:Clone()
        newBullet.CFrame = tool.Handle.CFrame
        newBullet.Parent = game.Workspace
        
        newBullet.Velocity = MouseHitLookVector * bulletSpeed
        
        ------ HERE Add it here
        
        wait(fireRate)
        shooting = false
    end
end)```
summer cosmos
#

ok

barren flame
#

It should work (Change Bullet.velocity to newBullet.velocity)

summer cosmos
#

the entire thing?

#

ok

barren flame
#

Yes

summer cosmos
#
tool.Activated:Connect(function()
    if shooting == false then
        shooting = true
        
        local player = game.Players:GetPlayerFromCharacter(tool.Parent)
        local MouseHitLookVector = RemoteFunction:InvokeClient(player)
        
        local newBullet = Bullet:Clone()
        newBullet.CFrame = tool.Handle.CFrame
        newBullet.Parent = game.Workspace
        
        newBullet.Velocity = MouseHitLookVector * bulletSpeed
        
        local BulletTouch
        BulletTouch = Bullet.Touched:Connect(function(hit)
            newBullet.velocity = 0
            if hit.Parent then
                local humanoid = hit.Parent:FindFirstChild("Humanoid")
                if humanoid then
                    humanoid.Health = humanoid.Health - bulletDamage
                end
            end
        end)
        
        wait(fireRate)
        shooting = false
    end
end)
print("Working")

BulletTouch:Disconnect()```
#

So I think before I test like on the last line

#

theres an underline on the BulletTouch

#

blue

barren flame
#
        BulletTouch = newBullet.Touched:Connect(function(hit) -- Oh yea, also this should be changed.
            newBullet.velocity = 0
            if hit.Parent then
                local humanoid = hit.Parent:FindFirstChild("Humanoid")
                if humanoid then
                    humanoid.Health = humanoid.Health - bulletDamage
                end
            end
        end)

BulletTouch:Disconnect() -- Move it here```
summer cosmos
#

it said attempted to index nil with disconnect

#

ok

barren flame
#

Oh yea also you were supposed to move it there

#

So, now your bullet should shoot out, cause some damage, disconnect the damage function

#

So that it doesn't work when already shot

summer cosmos
#

it works to shoot but it still doesn't do damage

#

i added a dummy with heaklth from the toolbox and it did nothing to it

#

it does hit it tho

#

so its working a lil 🙂

barren flame
#

Nice

#

What I usually like to do is to print some things out, like to make sure they aren't nil or anything.

#

Like try adding some print statements to print different things, like:

print(humanoid.Health .. " Health")

#

or print(hit .. " hit")

#

Because if one of those are nil, it won't work

summer cosmos
#

ok

#

i decided to add a zombie cause the rigs were built weird

#

i added print("Success") under the humanoid.Health - bulletDamage part and it didnt print it

#

so that part doesn't seem to be working

barren flame
#

Wait try do it again

proper spokeBOT
#

studio** You are now Level 11! **studio

barren flame
#

but print humanoid.Health

#

Does it index nil?

summer cosmos
#

it didn't print or index anything

barren flame
#

hmm

#

that means that it didn't function at all...

golden sphinx
#

ngl using raycasting would be better for guns

summer cosmos
#

ye

barren flame
#

Ok wait one minute lemme try something, I think i've got the notation wrong

summer cosmos
#

note the original script was from a video where they made a game in 10 minutes lol

#

I decided to try and use that to make a small game for myself with my own weapons like a banana

golden sphinx
#

ah i see

barren flame
#

I think maybe you should remove the checker of hit.Parent

#

Huh, because it all looks fine to me

summer cosmos
#

ok

#

lemme see what happens

barren flame
#

Nono, dont actually

#

I tried in studio, it worked fine on me

#

I'll try the dummy

summer cosmos
#

it did damage?

#

i think the issue is that it can't really find the humanoid or anything

#

like the way the video did it, i altered a lil to my script but

barren flame
#

Yea it worked fine to my avatar i meant

summer cosmos
#

i dont think its actually getting the humanoid

barren flame
#

I made it so that it wil print everything that is touching

summer cosmos
#

did it work

#

like it printed thjat it hit the part

barren flame
#

Yea, along with the damage

#

I will position him onto the brick

#

Nothing at all!

#

It only works in the player

#

SO it should be fine if you are shooting other people

#

But not rigs

summer cosmos
#

oh

#

like it did damage to a player but not a rig

#

or it will do damage to a player if there were players

barren flame
#

Yea

summer cosmos
#

alright tysm for all your help 🙂

barren flame
#

You are welcome

#

I think if you look this up, it is solveable, I think it is something to do with the rig

#

But that is another question

#

Bye!