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

Why is my gun not refilling the ammo when I reload it?

Asked by
Mr_Unlucky 1085 Moderation Voter
5 years ago

The reloading part doesn't work. It's on line 55. What it's supposed to do is that Bullets.Value will be equal to MaxBullets.Value. However, it doesn't refill it to that value and stays the same. Help! There's no errors either.

--//Variables
local Tool = script.Parent.Parent
local ScriptsFolder = Tool.Scripts
local AnimationsFolder = Tool.Animations
local SettingsFolder = Tool.Settings
local HoldAnimation = AnimationsFolder:WaitForChild("HoldAnimation")
local ReloadAnimation = AnimationsFolder:WaitForChild("ReloadAnimation")
local Handle = Tool.Handle
local GunfireSound = Handle.FireSound
local ReloadSound = Handle.ReloadSound
local Player = game:GetService("Players").LocalPlayer
local Mouse = Player:GetMouse()
local RemoteEvents = game:GetService("ReplicatedStorage").RemoteEvents
local PlayAnimation = RemoteEvents.PlayAnimation
local StopAnimation = RemoteEvents.StopAnimation
local CreateBullet = RemoteEvents.CreateBullet
local Reloading = false
local Debounce = false
local Equipped = false
--//Settings Variables
local FiringRate = SettingsFolder:WaitForChild("FiringRate")
local Bullets = SettingsFolder:WaitForChild("Bullets")
local MaxBullets = SettingsFolder:WaitForChild("MaxBullets")
local Accuracy = SettingsFolder:WaitForChild("Accuracy")
local Damage = SettingsFolder:WaitForChild("Damage")
--//Events
Tool.Equipped:Connect(function()
    PlayAnimation:FireServer(HoldAnimation)
    Equipped = true
end)

Tool.Unequipped:Connect(function()
    StopAnimation:FireServer(StopAnimation)
    Equipped = false
end)

Tool.Activated:Connect(function()
    if Debounce == false then
        Debounce = true
        Shoot()
        wait(FiringRate.Value)
        Debounce = false
    end
end)

function Shoot()
    if Bullets.Value > 0 and Reloading == false and Equipped == true then
        local ray = Ray.new(Handle.CFrame.p, (Mouse.Hit.p - Handle.CFrame.p + Vector3.new(math.random(0-Accuracy.Value,Accuracy.Value),math.random(0-Accuracy.Value,Accuracy.Value),0)).unit * 300)
        local part, position = workspace:FindPartOnRay(ray, Player.Character, false, true)
        CreateBullet:FireServer(Tool,position,part,Damage.Value)
        Bullets.Value = Bullets.Value - 1
        GunfireSound:Play()
    end
end
function onKeyPress(actionName, userInputState, inputObject) --This is the reload part.
    if userInputState == Enum.UserInputState.Begin and Reloading == false then
        Reloading = true
        PlayAnimation:FireServer(ReloadAnimation)
        ReloadSound:Play()
        wait(3)
        Bullets.Value = MaxBullets.Value
        Reloading = false
    end
end

game.ContextActionService:BindAction("keyPress", onKeyPress, false, Enum.KeyCode.R)
0
Why are you playing animations on the server? Play them on the client, they replicate. User#19524 175 — 5y
0
When I did an animation on client-sided it didn't work. Either the tutorial I looked at was outdated or I did something wrong. I tried doing a remote event and it replicated on other clients. Mr_Unlucky 1085 — 5y
0
But remotes are quite latent, it takes time for data to be sent from one side to the other, and you're putting only more work for the server. And no offense but you should work on consistency a bit. You use local variables for everything yet your function values are stored in global variables. User#19524 175 — 5y
0
kk thanks Mr_Unlucky 1085 — 5y
0
There doesn't look to be anything immediately wrong with your reload code (aside from running your animations on the server). Provided that Bullets and MaxBullets are both ValueBase values, and everything here is valid - then there should be no issues. I would go back and double check your setup moreover the actual reloading code. SummerEquinox 643 — 5y

Answer this question