I am working on a simple gun for a friend and at the moment all i'm trying to do is make it fire a bullet. The only thing that I can't figure out is why in the heck the bullet won't even fall to the ground. It just stays there like it's anchored but its not. I have tried everything, please help. (I am not anywhere near done with the gun.)
local handle = script.Parent.Handle local player = game.Players.LocalPlayer local mouse = player:GetMouse() script.Parent.Activated:connect(function() local bullet = Instance.new("Part",game.Workspace) local bulletPos = script.Parent.FirePart.Position local bodyVelocity = Instance.new("BodyVelocity",bullet) bullet.FormFactor = "Custom" bullet.Size = Vector3.new(.2,.2,.2) bullet.CanCollide = false bullet.Anchored = false bullet.TopSurface = "Smooth" bullet.BottomSurface = "Smooth" bullet.CFrame = CFrame.new(bulletPos,mouse.Hit.p) bullet.CFrame = bullet.CFrame + bullet.CFrame.lookVector * 3 bodyVelocity.velocity = bullet.CFrame.lookVector * 200 end)
Try This:
local tool = script.Parent local handle = tool:WaitForChild("Handle") local players = game:GetService("Players") local player = players.LocalPlayer repeat wait() until player:GetMouse() local mouse = player:GetMouse() function createBullet() local bullet = Instance.new("Part", game.Workspace) bullet.FormFactor = "Custom" bullet.Size = Vector3.new(.2,.2,.2) bullet.CanCollide = false bullet.Anchored = false bullet.TopSurface = "Smooth" bullet.BottomSurface = "Smooth" return bullet end tool.Activated:connect(function() local bullet = createBullet() local bulletPos = tool:WaitForChild("FirePart").Position local bodyVelocity = Instance.new("BodyVelocity") bullet.CFrame = CFrame.new(bulletPos, mouse.Hit.p) bodyVelocity.velocity = bullet.CFrame.lookVector * 200 bodyVelocity.Parent = bullet end)
Added a createbullet function so its easy to change, and make sure you add bodyvelocity after you say bullet.CFrame = CFrame.new(bulletPos, mouse.Hit.p) and I made it a little more efficient by using :WaitForChild(), game:GetService, functions, and a repeat/until to wait for mouse, also a tool variable