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:

01CanReload = false
02 
03function OnEquipped(playerMouse)
04    CanReload = true
05    local clone = GUI:Clone()
06    local TextLabel = clone:WaitForChild("TextLabel")
07    TextLabel.Text = Mag.Value.."/"..Mag.MaxValue
08    clone.Parent = Player.PlayerGui
09    Mouse = playerMouse
10    ExpectingInput = true
11    UpdateMouseIcon()
12end
13 
14function OnUnequipped()
15    CanReload = false
View all 66 lines...

Server script:

1ReloadEvent.OnServerEvent:Connect(function(Player,Tool)
2    local Mag = Tool:WaitForChild("Mag")
3    if not Mag then return end
4        if Mag.Value <= 0 then
5            Mag.Value = Mag.MaxValue - 1
6        else
7            Mag.Value = Mag.MaxValue
8    end
9end)

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.

1ReloadEvent.OnServerEvent:Connect(function(Player,Tool)
2    local Mag = Tool:FindFirstChild("Mag") -- instead of local Mag = Tool:WaitForChild("Mag")
3    if not Mag then return end
4        if Mag.Value <= 0 then
5            Mag.Value = Mag.MaxValue - 1
6        else
7            Mag.Value = Mag.MaxValue
8    end
9end)

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