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

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

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

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.

1 comment

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.

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