#How to script it so when a bullet hits a player, it does damage
131 messages · Page 1 of 1 (latest)
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
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.
It does work tho
Oh wait it does? Is there a damage?
I can shoot it and it shoots the bullet
Oh, does it stop anywhere in the ground?
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
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?
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
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)```
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
ok one sec
Above the print function
it works but how should i test if it can do damage?
Hmm, you would usually test it on a rig which could take damage
It should work exactly the same for a player
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
Ok wait let me think
ok
Oh yea, you do need it to stop on impact
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```
ok i'll try it
But i'm not sure if it will work
sadly didn't work
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
yes I think
So, we have to move it inside the shooter function
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)```
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)```
ok
It should work (Change Bullet.velocity to newBullet.velocity)
Yes
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
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```
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
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 🙂
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
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
Wait try do it again
** You are now Level 11! **
it didn't print or index anything
ngl using raycasting would be better for guns
ye
Ok wait one minute lemme try something, I think i've got the notation wrong
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
ah i see
I think maybe you should remove the checker of hit.Parent
Huh, because it all looks fine to me
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
Yea it worked fine to my avatar i meant
i dont think its actually getting the humanoid
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
oh
like it did damage to a player but not a rig
or it will do damage to a player if there were players
Yea
alright tysm for all your help 🙂