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

How Do I Properly Set Up An Ammo System in Roblox?

Asked by 6 years ago

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)




1 answer

Log in to vote
1
Answered by
Radstar1 270 Moderation Voter
6 years ago
Edited 6 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:

local tool = script.Parent
local player = game:GetService("Players").LocalPlayer
local gui = game.Players.LocalPlayer.PlayerGui.AmmoGui.AmmoBar
local Equipped = false
local ammo = 5 -- MAX AMMO IN CLIP Outside of the Tool.Equipped

tool.Equipped:Connect(function(mouse)
    Equipped = true
    gui.AmmoInClip.Text = ammo
end)
tool.Unequipped:Connect(function()
    Equipped = false
end)

mouse.Button1Down:connect(function()
        if ammo >= 1 and Equipped then -- if ammo is over or equal to 1, shoot, and if weapon is equipped
        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)




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

script.Parent.ClickDetector.MouseClick:Connect(function(Player)
    if Player.Backpack:FindFirstChild(Tool.Name) then -- or wherever you put the tool
        Tool.Ammo = 5
    end
end)

0
Thank you sooo much! Lol I never thought of using a number value instead of values inside the script!!! Thank you so much again! TheFierceWaffle 64 — 6y
0
Keep scripting, you will get much better. Radstar1 270 — 6y
0
Hello, MyosotisArae 0 — 2y
0
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 ? MyosotisArae 0 — 2y
View all comments (2 more)
0
a bug, I mean MyosotisArae 0 — 2y
0
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) MyosotisArae 0 — 2y
Ad

Answer this question