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

How do i make a gun that shoots not damage me?

Asked by 7 years ago

So I have this code: tool = script.Parent player = game.Players.LocalPlayer mouse = player:GetMouse() tool.Activated:connect(function() local Part = Instance.new("Part", workspace) Part.Size = Vector3.new(0.1,0.1,0.1) Part.Shape = ("Ball") Part.Position = Vector3.new(-3.5,0.5,4.5) Part.CFrame = tool.Handle.CFrame Part.CFrame = CFrame.new(Part.Position,mouse.Hit.p) local v = Instance.new("BodyVelocity", Part) v.velocity = Part.CFrame.lookVector *90 v.maxForce = Vector3.new(math.huge, math.huge, math.huge) Part.Touched:connect(function(hit) if hit.Parent ~= player.Name and hit.Parent:FindFirstChild("Humanoid") ~= nil then hit.Parent:FindFirstChild("Humanoid").Health = hit.Parent:FindFirstChild("Humanoid").Health -25 end end) end)

But the problem is that when I shoot the tool, the bullet damages me too.

2 answers

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

First of all please next time indent your code properly. And I would assume this code is in a server script since it can only be ran server wide through that. Next, please never use game.Players.LocalPlayer in a server script, which is starting to make me think you are starting off wrong but anyway.

tool = script.Parent 
player = tool.Parent.Parent -- In server script the game.Players.LocalPlayer is not allowed, so it takes the tool and finds the player. 
mouse = player:GetMouse()
 tool.Activated:connect(function() 
local Part = Instance.new("Part", game.Workspace) --I prefer it more that way instead of just workspace
Part.Size = Vector3.new(0.1,0.1,0.1)
 Part.Shape = ("Ball") 
Part.Position = Vector3.new(-3.5,0.5,4.5) 
Part.CFrame = tool.Handle.CFrame +  CFrame.new(mouse.Hit.p) --This looks better but may error
local v = Instance.new("BodyVelocity", Part)
 v.velocity = Part.CFrame.lookVector *90 
v.maxForce = Vector3.new(math.huge, math.huge, math.huge) 
Part.Touched:connect(function(hit) 
if hit.Parent ~= player and hit.Parent:FindFirstChild("Humanoid") ~= nil then hit.Parent:FindFirstChild("Humanoid").Health = hit.Parent:FindFirstChild("Humanoid").Health -25 end end) end)

Overall I would consider using raycasting and remote events to make this script better, if this helped even just a little please give a thumbs up and accept my answer! If you are confused of any sort just comment on the answer. Glad to help!

Ad
Log in to vote
0
Answered by 7 years ago

Add a wait(.5) so when your projectile has time to leave the gun before it starts detecting for a collision.

Answer this question