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

Gun script not working?

Asked by
Scriptecx 124
9 years ago

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)
0
Why is the bullet suppose to fall to the ground? magiccube3 115 — 9y
0
No, it does not move at all. It just sits there like it is anchored. Scriptecx 124 — 9y

1 answer

Log in to vote
0
Answered by 9 years ago

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

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. Scriptecx 124 — 9y
0
Thats what I said... DevScripting 92 — 9y
Ad

Answer this question