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

Value.Changed:Connect(function() only works the first time, what did I do wrong?

Asked by 4 years ago

I just came across the FastCast module the other day and wanted to add onto the script so that the gun could have an ammo GUI and also have a reload system that can keep one bullet in the chamber. The gun starts off with 0 bullets in the magazine. When I reload it, it reloads but it doesn't subtract the 1 bullet that isn't in the chamber. I fire a couple shots, and when I try to reload again, the game reload event fires but the Mag.Changed:Connect(function() doesn't connect. Here's what I tried to do:

Client Script:

CanReload = false

function OnEquipped(playerMouse)
    CanReload = true
    local clone = GUI:Clone()
    local TextLabel = clone:WaitForChild("TextLabel")
    TextLabel.Text = Mag.Value.."/"..Mag.MaxValue
    clone.Parent = Player.PlayerGui
    Mouse = playerMouse
    ExpectingInput = true
    UpdateMouseIcon()
end

function OnUnequipped()
    CanReload = false
    if Player.PlayerGui:FindFirstChild("Magazine") then
    Player.PlayerGui.Magazine:Destroy()
    end
    ExpectingInput = false
    UpdateMouseIcon()
end

Tool.Equipped:Connect(OnEquipped)
Tool.Unequipped:Connect(OnUnequipped)

local reloaddebounce = false

UIS.InputBegan:Connect(function(Input)
    if Input.KeyCode == Enum.KeyCode.R then
        if CanReload then
            if not reloaddebounce then
                print("reloading")
                reloaddebounce = true
                Reload:FireServer(Tool)
                wait(1)
                reloaddebounce = false
            end
        end
    end
end)

UserInputService.InputBegan:Connect(function (input, gameHandledEvent)
    if gameHandledEvent or not ExpectingInput then
        return
    end

    if input.UserInputType == Enum.UserInputType.MouseButton1 and Mouse ~= nil then
        local FireDirection = (Mouse.Hit.Position - FirePointObject.WorldPosition).Unit
        local Mag = Tool:FindFirstChild("Mag")

        if not Mag then return end

        if Mag.Value <= 0 then
            print("Out Of Ammo")
        else
            MouseEvent:FireServer(FireDirection)
            Mag.Value = Mag.Value - 1
        end
    end
end)

Mag.Changed:Connect(function()
    if Player.PlayerGui:FindFirstChild("Magazine") and Player.PlayerGui:FindFirstChild("Magazine"):FindFirstChild("TextLabel") then
Player.PlayerGui.Magazine.TextLabel.Text = Mag.Value.."/"..Mag.MaxValue
    end
end)

Server script:

ReloadEvent.OnServerEvent:Connect(function(Player,Tool)
    local Mag = Tool:WaitForChild("Mag")
    if not Mag then return end
        if Mag.Value <= 0 then
            Mag.Value = Mag.MaxValue - 1
        else
            Mag.Value = Mag.MaxValue
    end
end)

1 answer

Log in to vote
0
Answered by 4 years ago

At first, try to use "FindFirstChild" instead of "WaitForChild", because if instance, that you try to wait, is already exists, the waiting will be endless.

ReloadEvent.OnServerEvent:Connect(function(Player,Tool)
    local Mag = Tool:FindFirstChild("Mag") -- instead of local Mag = Tool:WaitForChild("Mag")
    if not Mag then return end
        if Mag.Value <= 0 then
            Mag.Value = Mag.MaxValue - 1
        else
            Mag.Value = Mag.MaxValue
    end
end)

Second, as i can understand, you tried to subtract value of "IntValue" or "NumberValue" class with local script. It won't work like that, you should create a "FireEvent" remote (or use your MouseEvent remote, if it handle the shot), so server can handle Mag value by itself.

0
I tried changing it to FindFirstChild and heeded no results. Also, line 4 and 5 is detecting if the ammo is at 0 so the server can reload the magazine minus 1 because there's no round in the chamber. The MouseEvent is handling the shots fired and subtracts 1 from the Mag.Value each time the player presses MouseButton1. Thanks for trying to help, though. LeedleLeeRocket 1257 — 4y
Ad

Answer this question