Why isn't this MouseButton1 script working?

I made a script that checks if MouseButton1 is down, and it would check how long it would be held down, but I’m having huge issues that affect my game.

Issues are Multiple Bombs are spawned instead of one (The Remote Event gets fired multiple times), and the effect that I’m going for, that’s supposed to make the bomb have a higher trajectory if the mouse is held longer doesn’t even work, as the trajectory is automatically set to high for some reason…

There are no errors in the console, so I’m not sure what to do.


Example in the video on what’s going on:

Here are the scripts from local to server:

local ActivationEvent = script.Parent:WaitForChild("ThrowBomb")
local tool = script.Parent
local player = game:GetService("Players"):GetPlayerFromCharacter(tool.Parent)
local mouse = player:GetMouse()

tool.Activated:Connect(function()
	local mousetime = 0
	local fired = false
	
	local function MouseButton1Down()
		print("MouseButton1 Function Fired")
		if not fired then
			print("fired MB1 event part 2")
			fired = true
			repeat task.wait(.1)
				mousetime += .1
			until mouse.Button1Up
		else
			return
		end
	end
	
	local function MouseButton1Up()
		mousetime = math.round(mousetime)
		if mousetime <= 1 or mousetime == 1 then
			ActivationEvent:FireServer(player, mouse.Hit, player.Character, 1)
		elseif mousetime == 2 then
			ActivationEvent:FireServer(player, mouse.Hit, player.Character, .8)
		elseif mousetime == 3 then
			ActivationEvent:FireServer(player, mouse.Hit, player.Character, .6)
		elseif mousetime == 4 then
			ActivationEvent:FireServer(player, mouse.Hit, player.Character, .4)
		elseif mousetime == 5 then
			ActivationEvent:FireServer(player, mouse.Hit, player.Character, .2)
		end
	end
	
	mouse.Button1Up:Connect(MouseButton1Up)
	mouse.Button1Down:Connect(MouseButton1Up)
end)
local ActivationEvent = script.Parent:WaitForChild("ThrowBomb")
local BombFuse = script.Parent:WaitForChild("Bomb Fuse")
local Explosion = script.Parent:WaitForChild("Explosion")

function calculateVelocity(startPos, endPos, mousetime)
	local G = (endPos - startPos) / mousetime + Vector3.new(0, workspace.Gravity * mousetime * 0.5, 0)

	return G
end

ActivationEvent.OnServerEvent:Connect(function(player, character, mouseHit, char, mousetime)
	local Bomb = script.Parent:WaitForChild("Handle"):Clone()
	Bomb.Parent = workspace:WaitForChild("ClonedBombs")
	Bomb.Name = "clonedBomb"
	
	Bomb.AssemblyLinearVelocity = calculateVelocity(Bomb.Position, mouseHit.Position, mousetime)
	Bomb.CanCollide = true
end)
2 Likes

Here you don’t appear to be calling Button1Down


Plus, why are you doing this?

1 Like

That didn’t completely fix it, multiple bombs still spawn and the trajectory isn’t working.

Also, I’m trying to make it so that the player can throw a bomb