Alright. I learned how to make a RayCasting gun using the Roblox Wiki. Next thing, I made a little ammo system. Basically when I equip the gun, the script sets the gun's max ammo to 5. When I click/shoot the gun, the "ammo" value is reduced by one. After that, I made it so if the ammo value is equal or more than 1, the gun will shoot. If it is not, it does nothing. I then connected it to a GUI which displays the ammo value. It works! Only one thing... every time I equip the gun, the ammo is set back to 5.//I would like to know how to make it so the ammo value will save every time I un-equip the gun. I would also like it to save in the event the gun was dropped unto the ground, and another player was to pick it up. I would also like to know how to make a "stored ammo" where ammo is added in the event the player does something, say, clicks a part. Thank you so much! --PS-I am very sorry my code is very unorganized, I am sorry if you cannot understand it.
local tool = script.Parent
local player = game:GetService("Players").LocalPlayer
local gui = game.Players.LocalPlayer.PlayerGui.AmmoGui.AmmoBar
tool.Equipped:connect(function(mouse)
local ammo = 5 -- MAX AMMO IN CLIP
gui.AmmoInClip.Text = ammo
mouse.Button1Down:connect(function()
if ammo >= 1 then -- if ammo is over or equal to 1, shoot
print("Mouse pressed!")
local ray = Ray.new(tool.Handle.CFrame.p, (mouse.Hit.p - tool.Handle.CFrame.p).unit * 300)
local part, position = workspace:FindPartOnRay(ray, player.Character, false, true)
local beam = Instance.new("Part", workspace)
beam.BrickColor = BrickColor.new("Yellow")
beam.FormFactor = "Custom"
beam.Material = "Neon"
beam.Transparency = 0.25
beam.Anchored = true
beam.Locked = true
beam.CanCollide = false
local distance = (tool.Handle.CFrame.p - position).magnitude
beam.Size = Vector3.new(0.3, 0.3, distance)
beam.CFrame = CFrame.new(tool.Handle.CFrame.p, position) * CFrame.new(0, 0, -distance / 2)
game:GetService("Debris"):AddItem(beam, 0.01)
if part then
local humanoid = part.Parent:FindFirstChild("Humanoid")
if not humanoid then
humanoid = part.Parent.Parent:FindFirstChild("Humanoid")
end
if humanoid then
humanoid:TakeDamage(30)
end
ammo = (ammo-1) -- Subtract - shot from ammo
gui.AmmoInClip.Text = ammo
end
end
end)
end)

6 comments
Thank you sooo much! Lol I never thought of using a number value instead of values inside the script!!! Thank you so much again!
Keep scripting, you will get much better.
Hello,
Perhaps this worked 3 years ago, but I tried this, and the value I modified in the local script (by the shoot) was unchanged when I read it from the server script (the part clicked). Is it a but I should report to Roblox ?
a bug, I mean
When I display it from the CommandBar, the value is right, though. So, this command does not give the same result in the script (Part:MouseClicked) than in the command bar : print(game.Players.MyosotisArae.Backpack.gun.nbAmmo.value)