Tool activated event firing when clicking UI

Hello. I am trying to create a gun script but I have seem to run in to a problem. I have a button for mobile players to press in order to reload, but when you press / click the button it just fires anyway. I saw another post similar to this and it had said something about the active property being disabled, I tried enabling it and disabling it but still it did not work correctly.

I have looked on numerous sites. Nothing has been found of this besides the one topic I read.

Here is the code for the local script.

local mouse = game.Players.LocalPlayer:GetMouse()
local ui = game.Players.LocalPlayer:WaitForChild("PlayerGui"):WaitForChild("Ammo").TextLabel

-- CUSTOM --
ammo = 17
fullmag = 17
-- CUSTOM -- 

local uis = game:GetService("UserInputService")
canshoot = true

script.Parent.Activated:Connect(function()
	if canshoot == true then
		if ammo > 0 then
			ammo = ammo - 1
			ui.Text = (ammo.." / ∞")
			script.Parent.Shoot:FireServer(mouse.Hit.p)
		else
			script.Parent.BlankEvent:FireServer()
		end
	end



end)

uis.InputBegan:Connect(function(key)
	if key.KeyCode == Enum.KeyCode.R then
		canshoot = false
		script.Parent.ReloadEvent:FireServer()
		wait(2)
		ammo = fullmag
		ui.Text = (ammo.." / ∞")
		canshoot = true


	end
end)

script.Parent.Equipped:Connect(function()
	ui.Visible = true
	ui.Parent.FrameI.Visible = true
	ui.Parent.Frame.Visible = true
	ui.Parent.reload.Visible = true
	script.Parent.Equip:FireServer()
end)

script.Parent.Unequipped:Connect(function()
	ui.Visible = false
	ui.Parent.FrameI.Visible = false
	ui.Parent.Frame.Visible = false
	ui.Parent.reload.Visible = false
	script.Parent.Uneqip:FireServer()
end)

ui.Parent.reload.MouseButton1Click:Connect(function()
	if canshoot == true then
		if ammo > 0 then
			ammo = ammo - 1
			ui.Text = (ammo.." / ∞")
			script.Parent.Shoot:FireServer(mouse.Hit.p)
		else
			script.Parent.BlankEvent:FireServer()
		end
	end



end)

If you know why or how to stop this, please reply. Thanks!

From what I can see, the reload gui seems to have the exact same code as the tool.activated event

ui.Parent.reload.MouseButton1Click:Connect(function()
	if canshoot == true then
		if ammo > 0 then
			ammo = ammo - 1
			ui.Text = (ammo.." / ∞")
			script.Parent.Shoot:FireServer(mouse.Hit.p)
		else
			script.Parent.BlankEvent:FireServer()
		end
	end
end)

I believe you meant to make it

ui.Parent.reload.MouseButton1Click:Connect(function()
    canshoot = false
	script.Parent.ReloadEvent:FireServer()
	wait(2)
	ammo = fullmag
	ui.Text = (ammo.." / ∞")
	canshoot = true
end)
2 Likes

Thank you for replying!

Just tried this out, and it works. Really appreciate it!

Anytime! I would also recommend adding a debounce to the code so you can only activate reloading once because from what I can see, there’s nothing stopping the player from spamming reload many times whilst it is reloading

1 Like

Thanks for the feedback.

I believe there is a debounce called “canshoot”, the player can only shoot while it is true and once the player reloads it is deactivated for 2 seconds
RobloxStudioBeta_NHpZPwiE0I

Yes but that debounce is only for shooting, there isn’t anything that stops the player from spamming the reload key or reload ui, which I don’t believe can really cause any issue to my knowledge, but typically I do it just incase, the only issues I would see is it you’re playing an animation/sound in that RemoteEvent, because this will cause the animation/sound to be spammable without a debounce, but again it’s personal preference. But if you would like to add a reloading debounce, I could help with that

1 Like

Alright, I see what you mean.

I will try to add this in then, thanks!

Alright, good luck! If you ever run into any issues, send a reply in this post and I’ll try to help!

1 Like