Ad
Log in to vote
0

When I unequip and equip my gun I get less ammo and get an error, can someone help me?

Asked by 3 days ago
Edited 3 days ago

So I have two problems with my script first of all I get an error. The error always happens when I unequip and equip the gun again. The error says "This Mouse is no longer active". The second problem is when I unequip and equip the gun again, I get less ammo. I would be happy if you could help me error:    ~~~~~~~~~~~~~~~~~

This Mouse is no longer active - Client - MainScript:24    Stack Begin - Studio   Script 'Players.FireTap1.Backpack.Deagle.MainScript', Line 24 - Studio - MainScript:24     Stack End




code: ~~~~~~~~~~~~~~~~~ local maxAmmo = 8 local ammo = maxAmmo local reloading = false local player = game:GetService("Players").LocalPlayer local playerGui = player:WaitForChild("PlayerGui") local textLabel = playerGui:WaitForChild("AmmoDisplay"):FindFirstChild("AmmoText") local mouse = nil local starterGui = game:GetService("StarterGui") script.Parent.Equipped:Connect(function(Mouse) textLabel.Visible = true local function reload() mouse = Mouse reloading = true wait(1) ammo = maxAmmo reloading = false end script.Parent.Activated:Connect(function() if ammo > 0 and not reloading then ammo = ammo -1 script.Parent.gunShot:Play() if Mouse.Target.Parent:FindFirstChild("Humanoid") then script.Parent.DealDamage:FireServer(Mouse.Target.Parent, 20) end elseif reloading == false then reload() script.Parent.reload:Play() script.Parent.gunShot:Stop() end while wait() do textLabel.Text = (ammo.. " / "..maxAmmo) end end) local tool = script.Parent local anim2 = Instance.new("Animation") local input = game:GetService("UserInputService") anim2.AnimationId = "6425472378" input.InputBegan:Connect(function(key) if key.KeyCode == Enum.KeyCode.R and reloading == false and ammo ~= maxAmmo then reload() script.Parent.reload:Play() end end) end) script.Parent.Unequipped:Connect(function(Mouse) mouse = nil textLabel.Visible = false end)

1 answer

Log in to vote
0
Answered by 3 days ago

First, you should organize your script a bit more to make it easier to see where it could've gone wrong. Second of all, NEVER use ":Connect()" inside of another ":Connect()": you risk creating far more errors, crashing, and much more because you're basically saying "run an instance of this function when you run" and unless you disconnect it, your running it multiple times.

Now, here's a cleaner version of your code (hopefully less buggy) with a few comments:

input = game:GetService("UserInputService");                                    --Gets input service
starterGui = game:GetService("StarterGui");                                     --Gets the starter GUI
player = game:GetService("Players").LocalPlayer;                                --Gets the player
playerGui = player:WaitForChild("PlayerGui");                                   --Gets the player's GUI display
textLabel = playerGui:WaitForChild("AmmoDisplay"):FindFirstChild("AmmoText");   --Gets the player's current ammo text

maxAmmo   = 8;          --The maximum amount of ammo the player can carry
ammo      = maxAmmo;    --The current amount of ammo the player is carrying
reloading = false;      --If the player is currently reloading
eqquiped  = false;      --So then reloading doesn't run when the gun isn't eqquiped


reload = function()
    reloading = true;   --Set to the reloading state
    wait(1);            --Pause before reloading all ammo
    ammo = maxAmmo;     --Reloads all current ammo
    reloading = false;  --Set to not reloading
end --Reloads the weapon

activate = function(Mouse)
    if (ammo > 0 and not reloading) then    --If there's ammo and the player isn't reloading
        ammo -= 1;                                                          --Takes away a bullet
        script.Parent.gunShot:Play();                                       --Plays a gunshot noise

        if Mouse.Target.Parent:FindFirstChild("Humanoid") then              --If a "humanoid" is found within the hit object
            script.Parent.DealDamage:FireServer(Mouse.Target.Parent, 20);   --Reqeust to deal damage
        end
    elseif not reloading then               --If not reloading (yet ammo is not greater than 0)
        reload();                                                           --Reload the weapon
        script.Parent.reload:Play();                                        --Play the reloading noise
        script.Parent.gunShot:Stop();                                       --Stop any existing gunshot noise
    end
end

script.Parent.Equipped:Connect(function(Mouse)
    eqquiped = true;                            --Eqquiped
    textLabel.Visible = true;                   --Displays current ammo
    --local tool = script.Parent; (Why is this needed?)
    --local anim2 = Instance.new("Animation"); (Your code isn't doing anything with this so just put it in yourself)
    --anim2.AnimationId = "6425472378"; (Just so you know, AnimationController:LoadAnimation() and Humanoid:LoadAnimation() is depricated; search for "AnimationController" to see the new changes)

    script.Parent.Activated:Connect(activate);  --Binds the activate function upon tool actiavation
end)

script.Parent.Unequipped:Connect(function(Mouse)
    eqquiped = false;                           --No longer eqquiped
    activate:Disconnect();                      --Unbinds the activation upon tool activation (the function no longer gets run when the tool is activated)
    textLabel.Visible = false;                  --Removes the current ammo display
end)

input.InputBegan:Connect(function(key)
    if key.KeyCode == Enum.KeyCode.R and reloading == false and ammo ~= maxAmmo and eqquiped then
        reload();                       --Reloads the weapon
        script.Parent.reload:Play();    --Plays reloading noise
    end
end)

updateText = coroutine.wrap(function()
    while wait() do --Pause to prevent freezing
        textLabel.Text = (ammo.. " / "..maxAmmo);   --Update text
    end
end) --Coroutines are like a seperate script: they run individually without having to wait for it to finish
updateText(); --Run the coroutine (I'd also reccomend using "RunService" instead of this as you would only need to update the text every frame)

Make sure to read it throughly, and compare it to your original code! It'll help you understand your mistakes!

If you have any questions or issues, contact me. ;)

0
Now I get an error on line 25 the error is:Players.FireTap1.Backpack.Deagle.MainScript:25: attempt to index nil with 'Target' FireTap1 5 — 2d
0
And I also get another error- on line 47: Players.FireTap1.Backpack.Deagle.MainScript:47: attempt to index function with 'Disconnect' FireTap1 5 — 2d
0
Two more things firstly I can't damage anyone anymore and when I unequip and equip again I get less ammo FireTap1 5 — 2d
Ad

Answer this question