Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Why doesn't my gun reload script work? It doesn't say anything about errors.

Here is the code I'm going to sleep I will see what you guys say tomorrow.

reloading=(false)
function reload(key)
if (reloading==(true)) then return end
reloading=(true)
key = (key:lower())
if(key==("e")) then
if(tool.Ammo.Value==(not 0)) then return end  -- I believe the problem is here, can still reload
animation=(tool.Parent.Humanoid:LoadAnimation(tool.Animation))
animation:Play()
wait((3))
tool.Ammo.Value=(20)
reloading=(false)
end
end
mouse.KeyDown:connect(reload)

5 comments

0

0 Um, why exactly did you put the "not 0" in Line #7? Also, keyDown is deprecated try using this instead: http://robloxdev.com/articles/Keyboard-Input-Methods

0

I believe OP did that to check if the bullets were not equal to 0. However it was done wrong lol

0

@incapaz right its totally wrong. I'm still confused on why he would want the bullets to not equal 0 if he is trying to play a reload animation...

0

No, OP wants to check if the bullets are not equal to zero, to stop the function. He wants a gun that will only reload if the bullet amount is zero.

0

@incapaz Oh snap you're right, I didn't see the return.

1 answer

###The problem with your script is on line 7. You're trying to check if the Ammo is not equal to 0. You're not doing that in reality.

#Booleans

###All scripters should know about booleans. They are true or false. However, there are truthy and falsey values. true is obviously a truthy value and false a falsey. But numbers and strings, since they are not nil or false, they are treated as true/truthy. So what line 7 is basically doing is:

if tool.Ammo.Value == false then

###You're checking if its value is equal to false. But according to that script of yours it is an IntValue or a NumberValue. To fix that, you can check if the value is greater than 0, or just not equal to 0 in general.

if tool.Ammo.Value ~= 0 then return end
--OR--
if tool.Ammo.Value > 0 then return end

###Final product:

local reloading = false -- use local variables 

local function reload(key, gpe)
    if reloading then return end -- you wouldn't need to check == true 
    if gpe then return end -- if chatting or something where text is being processed 

    reloading = true -- we are reassigning the first reloading variable, don't add local
    
    if key.KeyCode == Enum.KeyCode.E then -- We will instead use UserInputService as KeyDown is an inconsistent way to get user input and is deprecated.


        if tool.Ammo.Value > 0 then return end -- fixed 

        local animation = tool.Parent.Humanoid:LoadAnimation(tool.Animation)
        animation:Play()
        wait(3)
        tool.Ammo.Value = 20
        reloading = false
    end
end
game:GetService("UserInputService").InputBegan:Connect(reload)

###The indentations in your code are worth noting. Don't remove the indentations that Studio gives you. It helps you and other readers to read your code better. Learn on writing clean code here

1 comment

0

Thx it worked

This discussion is preserved as it was. New answers, comments, and votes are closed.