Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Gun script not working?

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)

2 comments

0

Why is the bullet suppose to fall to the ground?

0

No, it does not move at all. It just sits there like it is anchored.

1 answer

✓ Accepted answer

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

2 comments

0

Your script didn't work, but I figured it out. There needs to be a small wait between creating the part and setting its properties. Thanks for trying though.

0

Thats what I said...

This discussion is preserved as it was. New answers, comments, and votes are closed.