Finding Current Cannon Issue

Hello, I’m working on a cannon and I’ve ran into a problem. I wanted to make it so I can shoot with any cannon only using one script but I’ve ran into a problem. I can’t seem to identify the cannon in the workspace(Well clueless on how too). Could anybody show me how I could achieve this so I don’t have to go into a alternative option. Please help :pray:.

Code:

-- Main --
RemoteEvent.OnServerEvent:Connect(function(Player,MousePosition)
	if not Equipped then return end
	if not Player.Character:GetAttribute('UseCannon',true) then
		return
	end
	-- Humanoid --
	local Humanoid = Player.Character:FindFirstChildOfClass('Humanoid')
	if not Humanoid or Humanoid.Health <= 0 then
		return
	end
	-- Fire Tick --
	if not ((tick() - FireTick) >= FireRate*TimeLimit) then return end
	FireTick = tick()
	-- Event Tick --
	if Events >= EventLimit and tick() - EventLimit < 1 then
		return
	end
	if tick() - EventLimit >= 1 then
		EventTick = tick()
		Events = 0
	end
	-- Find Current Cannon --
	for _,Cannons in pairs(workspace:GetDescendants()) do
		Cannons = Player.Character:GetAttribute('CurrentCannon',workspace:FindFirstChild('Cannon'))
		CurrentCannon = Cannons
	end
	-- Fire --
	Events += 1
	Fire(Player,MousePosition,CurrentCannon)
end)
---

Sorry but I don’t quite understand the issue :sweat_smile: could you elaborate a little more?

Also it seems like there’s more code, could you show us the whole code?

I’m tryna identify whats the current cannon the player is using. But I’m clueless on how to do that.

-- Services --
local Players = game:GetService('Players')
local ReplicatedStorage = game:GetService('ReplicatedStorage')
-- Remotes --
local RemoteFolder = ReplicatedStorage:WaitForChild('RemoteEvents')
local RemoteEvent = script:WaitForChild('RemoteEvent')
local GunEvent = RemoteFolder:WaitForChild('GunEvent')
-- States --
local Equipped = false
local CurrentCannon = nil
local FireRate,TimeLimit = 0.7,2/3
local EventLimit,Events = 10,0
local FireTick,EventTick = tick(),tick()
---

-- Functions --
function Fire(Player,MousePosition,Start)
	-- Positions --
	local StartPosition = Start.Muzzle.WorldPosition
	local Velocity = 500
	local Direction = CFrame.new(StartPosition,MousePosition)
	Direction = Direction.LookVector * Velocity
	--
	local Range = 3000
	-- Fire Sound --
	local Sound = Instance.new('Sound',Start.Sounds)
	Sound.Name = 'Fire'
	Sound.SoundId = 'rbxassetid://2228998125'
	Sound:Play()
	Sound.Ended:Connect(function()
		Sound:Destroy()
	end)
	-- Fire Event --
	GunEvent:FireAllClients(Player.Character,StartPosition,Direction,Range,nil,'Cannon')
	--
	local StartTime = tick()
	local LastPosition = StartPosition
	-- Filters --
	local Blacklist = {Player.Character}
	local RayFilter = RaycastParams.new()
	RayFilter.FilterType = Enum.RaycastFilterType.Blacklist
	-- Insert To Table --
	for _,Char in pairs(workspace:GetChildren()) do
		if Char:FindFirstChildOfClass('Humanoid') then
			for _,Accessory in pairs(Char:GetChildren()) do
				if Accessory:IsA('Accessory') then
					table.insert(Blacklist,Accessory.Handle)
				end
			end
		end
	end
	--
	RayFilter.FilterDescendantsInstances = Blacklist
	-- Range --
	--
	while Range > 0 do
		local TimeLength = (tick() - StartTime)
		local CurrentPosition = StartPosition + (Direction * TimeLength)
		local Distance = (LastPosition - CurrentPosition).Magnitude
		Range -= Distance
		-- Ray --
		local Ray = workspace:Raycast(LastPosition,CurrentPosition - LastPosition,RayFilter)
		if Ray == nil then
			Ray = workspace:Raycast(CurrentPosition,LastPosition - CurrentPosition,RayFilter)
		end
		if Ray then
			local Hit = Ray.Instance
			CurrentPosition = Ray.Position
			-- Explosion -- --
			local Explosion = Instance.new('Explosion',Hit)
			Explosion.Position = CurrentPosition
			-- Model --
			local Model = Hit:FindFirstAncestorOfClass('Model')
			if Model then
				-- Explode --
				if Hit:IsA('BasePart') then
					Hit:SetNetworkOwner(nil)
					Hit.CanCollide = true
					Hit.Anchored = false
				end
			end
			break
		end
		LastPosition = CurrentPosition
		task.wait()
	end
end
---

-- Main --
RemoteEvent.OnServerEvent:Connect(function(Player,MousePosition)
	if not Equipped then return end
	if not Player.Character:GetAttribute('UseCannon',true) then
		return
	end
	-- Humanoid --
	local Humanoid = Player.Character:FindFirstChildOfClass('Humanoid')
	if not Humanoid or Humanoid.Health <= 0 then
		return
	end
	-- Fire Tick --
	if not ((tick() - FireTick) >= FireRate*TimeLimit) then return end
	FireTick = tick()
	-- Event Tick --
	if Events >= EventLimit and tick() - EventLimit < 1 then
		return
	end
	if tick() - EventLimit >= 1 then
		EventTick = tick()
		Events = 0
	end
	-- Find Current Cannon --
	for _,Cannons in pairs(workspace:GetDescendants()) do
		Cannons = Player.Character:GetAttribute('CurrentCannon',workspace:FindFirstChild('Cannon'))
		CurrentCannon = Cannons
	end
	-- Fire --
	Events += 1
	Fire(Player,MousePosition,CurrentCannon)
end)
---

I would do something like:

print(currentCannon.Name)

For example