How to fire multiple ClickEvents

Hey folks, so I have a system where you can blow up a bridge, and you can also repair it afterwards. The problem here is that you can only do it once, and I don’t know why. The parts of the bridge are stored in a model.

Also; The BridgeHelperPart is a part that lets you drive on roads with cars and so on, it has nothing to do with the bridge.

BridgeDetonateHandler

local detect = script.Parent.ClickDetector

local bridge = game.Workspace.Bridge
local explosionPart = bridge.PrimaryPart

local RS = game:GetService('ReplicatedStorage')
local BridgeHelperPart = game.Workspace.RoadHelperParts.BRIDGE_PART

local function detonateBridge()
	local newBridge = bridge:Clone()
	newBridge.Parent = RS
	
	for i, v in pairs(bridge:GetChildren()) do
		if v:IsA('BasePart') then
			v.Anchored = false
		end
	end
	
	BridgeHelperPart.CanCollide = false
	 
	local explosion = Instance.new('Explosion')
	explosion.BlastRadius = 10
	explosion.ExplosionType = Enum.ExplosionType.NoCraters
	explosion.Position = explosionPart.Position
	explosion.Parent = bridge
end

detect.MouseClick:Connect(function()
	detonateBridge()
end)

BridgeRepairHandler

local detect = script.Parent.ClickDetector

local bridge = game.Workspace.Bridge
local RS = game:GetService('ReplicatedStorage')

local originalBridgePos = bridge.PrimaryPart.Position
local BridgeHelperPart = game.Workspace.KRAMPF.BRIDGE_PART

local function repairBridge()
	bridge:Destroy()
	
	local clonedBridge = RS:WaitForChild('Bridge')
	clonedBridge.Parent = game.Workspace
	clonedBridge:MoveTo(originalBridgePos)
	
	for i, v in pairs(clonedBridge:GetChildren()) do
		if v:IsA('BasePart') then
			v.Anchored = true
			v.CanCollide = true
		end
	end
	print('Bridge moved')
	BridgeHelperPart.CanCollide = true
end

detect.MouseClick:Connect(function()
	repairBridge()
end)

Thank you for your help!

  1. Is your problem being that the bridge is destroyed but doesn’t repair?
  2. Is your problem being that you can destroy and repair the bridge only once?

If 1, are your scripts within a single part or two parts? If it’s within a single part, you might wanna check if your bridge is being destroyed before it can be cloned into ReplicatedStorage.

If 2, try using a bool value and use an if statement to check for the bool value and execute the function based off of it. Example:
Detonate

local Broken = path.to.boolValue
-- code

detect.MouseClick:Connect(function()
        If Broken.Value == false then
               Broken.Value = true
               detonateBridge()
        end
end)

Repair

local Broken = path.to.boolValue
-- code

detect.MouseClick:Connect(function()
        If Broken.Value == true then
               Broken.Value = false
               repairBridge()
        end
end)
1 Like

It was indeed scenario number 2, sorry if I didn’t describe it very well. But thanks to you, it worked!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.