Answered by
7 years ago Edited 7 years ago
Before I get into this, make sure you learn how to tab your script properly. Makes reading it and fixing problems much easier.
Also in order to make your script cleaner it's better not to connect two functions inside of each other like you did with the Tool.Equipped
then the Mouse.ButtonClick
Instead
Make a variable named Equipped and change it to true once and once you click if that variable is true the rest will fire. I've put it inside of the code for you.
Actual Problem
So basically you put local ammo inside of tool.Equipped which makes the ammo 5 everytime you equip the weapon.
So take the variable local ammo outside of the tool.Equipped function.
and make it like so:
01 | local tool = script.Parent |
02 | local player = game:GetService( "Players" ).LocalPlayer |
03 | local gui = game.Players.LocalPlayer.PlayerGui.AmmoGui.AmmoBar |
07 | tool.Equipped:Connect( function (mouse) |
09 | gui.AmmoInClip.Text = ammo |
11 | tool.Unequipped:Connect( function () |
15 | mouse.Button 1 Down:connect( function () |
16 | if ammo > = 1 and Equipped then |
17 | print ( "Mouse pressed!" ) |
18 | local ray = Ray.new(tool.Handle.CFrame.p, (mouse.Hit.p - tool.Handle.CFrame.p).unit * 300 ) |
19 | local part, position = workspace:FindPartOnRay(ray, player.Character, false , true ) |
20 | local beam = Instance.new( "Part" , workspace) |
21 | beam.BrickColor = BrickColor.new( "Yellow" ) |
22 | beam.FormFactor = "Custom" |
23 | beam.Material = "Neon" |
24 | beam.Transparency = 0.25 |
27 | beam.CanCollide = false |
28 | local distance = (tool.Handle.CFrame.p - position).magnitude |
29 | beam.Size = Vector 3. new( 0.3 , 0.3 , distance) |
30 | beam.CFrame = CFrame.new(tool.Handle.CFrame.p, position) * CFrame.new( 0 , 0 , -distance / 2 ) |
32 | game:GetService( "Debris" ):AddItem(beam, 0.01 ) |
34 | local humanoid = part.Parent:FindFirstChild( "Humanoid" ) |
36 | humanoid = part.Parent.Parent:FindFirstChild( "Humanoid" ) |
39 | humanoid:TakeDamage( 30 ) |
42 | gui.AmmoInClip.Text = ammo |
If you want to learn how to save the actual ammo inside of it.
Then all you would have to do is put NumberValue inside of the tool named:
"Ammo" and use that instead of the variable named 'Ammo' that you have inside of the script.
Decreasing this number value will keep it constant.
If you want to make something reload when you click it.
You could add a click detector to like a brick(ammo pack) on the ground.
Then inside of the brick make a script that says
1 | script.Parent.ClickDetector.MouseClick:Connect( function (Player) |
2 | if Player.Backpack:FindFirstChild(Tool.Name) then |