✓ Accepted answer
While I do not like the method you went about to write this, there were only a couple problems in your code:
- Anchoring the part
- Not setting the MaxExtents
- Not breaking the joints (this fixes some issues with BodyVelocities and parts)
So, in effect, you just need to make sure the part is Unanchored, the max extents are set to Vector3.new(math.huge,math.huge,math.huge), and you need to break the joints of the part.
Here's the edited code:
local player = game.Players.LocalPlayer
mouse.KeyDown:connect(function(key)
key = key:lower()
if key == "f" then
if game.Workspace:FindFirstChild("Pew " .. player.Character.Name) then
while true do
wait()
end
end
print("Fired")
local p = Instance.new("Part", workspace)
local b = Instance.new("BodyVelocity", p)
p.Name = ("Pew " .. player.Character.Name)
p.Shape = ("Ball")
p.Size = Vector3.new(2.5,2.5,2.5)
p.Anchored = false
p.Position = player.Character.HumanoidRootPart.Position + player.Character.HumanoidRootPart.CFrame.lookVector * 5
p.CanCollide = false
b.MaxForce = Vector3.new(1/0,1/0,1/0)
b.Velocity = p.CFrame.lookVector * 5
p:BreakJoints()
p.Touched:connect(function(part)
p:Destroy()
local parent = part.Parent
if game.Players:GetPlayerFromCharacter(parent) then
parent.Humanoid.Health = parent.Humanoid.Health - 60
wait()
end
end)
wait(5)
p:Destroy()
end
end)
Here's a much better version of the code using cleaner, more up to date mechanics:
local player =game.Players.LocalPlayer
local userinput =game:GetService("UserInputService")
local bulletspeed =5
local lifetime =5
local damage =60
local debounce =true
local function shoot()
debounce =false
local rootpart =player.Character:FindFirstChild("HumanoidRootPart")
local rootcf =rootpart and rootpart.CFrame
local firedir =rootcf.lookVector*bulletspeed
local bullet =Instance.new("Part",workspace)
local vel =Instance.new("BodyVelocity",bullet)
bullet.Size =Vector3.new(2.5,2.5,2.5)
bullet.Name ="Bullet"
bullet.Shape ="Ball"
bullet.CFrame =rootcf*CFrame.new(0,0,-3)
bullet.CanCollide =false
vel.MaxForce =Vector3.new(1/0,1/0,1/0)
vel.Velocity =firedir
bullet:BreakJoints()
bullet.Touched:Connect(function(hit)
local hum =hit.Parent:FindFirstChild("Humanoid") or hit.Parent.Parent:FindFirstChild("Humanoid")
if hum then
local hitchar =hum.Parent
if hitchar then
local hitplayer =game.Players:GetPlayerFromCharacter(hitchar)
if hitplayer and hitplayer~=player then
debounce=true
bullet:Destroy()
hum:TakeDamage(damage)
end
end
end
end)
if lifetime and type(lifetime)=="number" and lifetime>0 then
delay(lifetime,function()
if bullet then
bullet:Destroy()
debounce=true
end
end)
end
end
userinput.InputBegan:Connect(function(input,processed)
if processed then return end
local keycode =input.KeyCode
if keycode==Enum.KeyCode.F then
shoot()
end
end)
3 comments
You should be using UIS or CAS to retrieve key input.
Are you getting any errors in the output window?
I'm not getting any errors in the output window