Hello there fine scripters of ROBLOX.
I have come across a dilemma. I decided it would be time to learn ROBLOX Lua and found a video that goes through on how to create a gun. Unfortunately, after following the tutorial almost word for word (changed the name of some variables, nothing large), I noticed that in the video the uploader's gun fired, but mine did not.
If you are going to help me please keep in mind that I am not new to programming, but am new to ROBLOX Lua, and although I Googled every event, function name, property etc. that I did not know, I only have a basic understanding of them, so I ask that you make it understandable.
I don't think there are code tags so here is the raw code. The documentation is for me by habit.
##############################################################################
player = game.Players.LocalPlayer --Sets player as the LocalPlayer, LocalPlayer gets the play the LocalScript is in
tool = script.Parent --Tool is set to parent of script
mouse = Player:GetMouse() --Returns mouse in use by the client
ammo = 10 --Ammo in gun
--Fire procedure
function fire ()
--If ammo is greater than 0 then a bullet is created
if (ammo > 0) then
bullet = Instance.new ("Part") --Creates a new instance of "Part"
bullet.Parent = game.Workspace --Sets parent to workspace
bullet.Name = "Bullet" --Sets name to "Bullet"
bullet.Shape = "Ball" --Sets shape to ball
bullet.Size = Vector3.new (1, 1, 1) --Sets size of bullet in X, Y, Z to 1
bullet.TopSurface = "Smooth" --Sets top surface of bullet to be smooth
bullet.BottomSurface = "Smooth" --Sets bottom surface of bullet to be smooth
bullet.BrickColor = BrickColor.new ("Toothpaste") --Sets color of bullet to "Toothpaste" which is cyanish
bullet.CanCollide = false --Makes it so bullet goes through stuff
bullet.CFrame = Gun.Handle.CFrame --CFrame in this case is the position, so I set position of bullet to the tool's handle
bullet.CFrame = CFrame.new (bullet.Position, mouse.Hit.p) --Sets position of bullet to it's position, and made it point to where the mouse is aiming
game.Debris:AddItem (bullet, 2) --Makes it so that 2 seconds after firing the bullet is removed
v = Instance.new ("BodyVelocity", bullet) --Create instance of "BodyVelocity" for bullet, which allows me to apply force to the bullet
v.velocity = Bullet.CFrame.lookVector * 90 --Sets speed that bullet moves at, in this case 90
v.maxForce = Vector3.new (math.huge, math.huge, math.huge) --Sets maximum speed that bullet can move at on the 3 axis to maximum
end
end
--Activated listener, fired when mouse click while holding tool
tool.Activated:connect (fire)
###############################################################################
Thank you to whoever helps me. Once again, the problem is that the gun does not fire, nothing happens when I press the mouse button.
Also the tutorial was Friaza Hyuga, "ROBLOX Scripting Tutorial 22 Making a Basic Gun" if that helps, I got to the 8:40 mark.
Thanks.
EDIT: This is a LocalScript inside the StarterPack |