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:
01 | CanReload = false |
02 |
03 | function 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() |
12 | end |
13 |
14 | function OnUnequipped() |
15 | CanReload = false |
Server script:
1 | ReloadEvent.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 |
9 | end ) |
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.
1 | ReloadEvent.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 |
9 | 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.