Beam Script not working correctly

Hey i have these scripts they work like i want but theres a glitch where the beam works only one time if i try the second time to use the beam it doesnt work anyone knows whats the problem and no theres nothing in the output except the 05:44:32.596 Missing one or more beams! (x2) - Server - Script:45
Server script

local tool = script.Parent
local remoteEvent = tool:WaitForChild("RemoteEvent")

local activeBeams = {}

local function cleanupBeams(player)
	if activeBeams[player] then
		for _, obj in pairs(activeBeams[player]) do
			if obj then
				obj:Destroy()
			end
		end
		activeBeams[player] = nil
	end
end

local function onRemoteEventFired(player, action, target)
	local character = player.Character
	if not character then
		warn("Character is invalid!")
		return
	end

	local head = character:FindFirstChild("Head")
	if not head then
		warn("Head is missing!")
		return
	end

	local laserStart1 = head:FindFirstChild("LaserStart")
	local laserStart2 = head:FindFirstChild("LaserStart2")
	if not laserStart1 or not laserStart2 then
		warn("Attachments LaserStart or LaserStart2 missing!")
		return
	end

	local beam1 = tool:FindFirstChild("Beam1")
	local beam2 = tool:FindFirstChild("Beam2")
	local beam3 = tool:FindFirstChild("Beam3")
	local beam4 = tool:FindFirstChild("Beam4")

	if not (beam1 and beam2 and beam3 and beam4) then
		warn("Missing one or more beams!")
		return
	end

	if action == "Start" then
		cleanupBeams(player)

		if not target or not target:FindFirstChild("Humanoid") then
			warn("Target is not a valid character!")
			return
		end

		local targetPart = target:FindFirstChild("Head") or target:FindFirstChild("Torso")
		if not targetPart then
			warn("Target has no suitable part for attachment!")
			return
		end

		local targetAttachment1 = Instance.new("Attachment")
		targetAttachment1.Parent = targetPart

		local targetAttachment2 = Instance.new("Attachment")
		targetAttachment2.Parent = targetPart

		beam1.Attachment0 = laserStart1
		beam1.Attachment1 = targetAttachment1
		beam1.Color = ColorSequence.new(Color3.fromRGB(255, 0, 0))
		beam1.Width0 = 0.2
		beam1.Width1 = 0.2
		beam1.FaceCamera = true
		beam1.Parent = head

		beam2.Attachment0 = laserStart2
		beam2.Attachment1 = targetAttachment2
		beam2.Color = ColorSequence.new(Color3.fromRGB(0, 255, 0))
		beam2.Width0 = 0.2
		beam2.Width1 = 0.2
		beam2.FaceCamera = true
		beam2.Parent = head

		beam3.Attachment0 = laserStart1
		beam3.Attachment1 = targetAttachment1
		beam3.Color = ColorSequence.new(Color3.fromRGB(0, 0, 255))
		beam3.Width0 = 0.2
		beam3.Width1 = 0.2
		beam3.FaceCamera = true
		beam3.Parent = head

		beam4.Attachment0 = laserStart2
		beam4.Attachment1 = targetAttachment2
		beam4.Color = ColorSequence.new(Color3.fromRGB(255, 255, 0))
		beam4.Width0 = 0.2
		beam4.Width1 = 0.2
		beam4.FaceCamera = true
		beam4.Parent = head

		activeBeams[player] = {
			beam1 = beam1,
			beam2 = beam2,
			beam3 = beam3,
			beam4 = beam4,
			targetAttachment1 = targetAttachment1,
			targetAttachment2 = targetAttachment2,
		}

		task.delay(5, function()
			cleanupBeams(player)
		end)

	elseif action == "Stop" then
		cleanupBeams(player)
	end
end

remoteEvent.OnServerEvent:Connect(onRemoteEventFired)

Local script

local tool = script.Parent
local remoteEvent = tool:WaitForChild("RemoteEvent")
local player = game.Players.LocalPlayer

local isHolding = false
local holdStartTime = 0

local function onKeyPress(input, gameProcessed)
	if input.KeyCode == Enum.KeyCode.Q and not gameProcessed then
		if not isHolding then
			isHolding = true
			holdStartTime = tick()

			local mouse = player:GetMouse()
			if mouse and mouse.Target then
				local target = mouse.Target.Parent
				if target:FindFirstChild("Humanoid") then
					local targetPosition = mouse.Target.Position
					remoteEvent:FireServer("Start", target)
				else
					isHolding = false
				end
			else
				isHolding = false
			end
		end
	end
end

local function onKeyRelease(input)
	if input.KeyCode == Enum.KeyCode.Q and isHolding then
		isHolding = false
		remoteEvent:FireServer("Stop")
	end
end

game:GetService("RunService").RenderStepped:Connect(function()
	if isHolding and tick() - holdStartTime > 5 then
		isHolding = false
		remoteEvent:FireServer("Stop")
	end
end)

game:GetService("UserInputService").InputBegan:Connect(onKeyPress)
game:GetService("UserInputService").InputEnded:Connect(onKeyRelease)

try changing this part to be like this :-

uhmm now it doesnt work at all and show the same thing in the output

1 Like

are you sure that the tool have the beams in it ?

2 Likes

this change will make it return if one of the beams is missing

1 Like

yeah there are all of the 4 beams im sure

1 Like

try checking for the beams in the activebeams table instead from checking the tool it self

im not sure what im supposed to do can you explain it better

1 Like

in this function you clear all the active beams for the player that is stored in

this table so i suggest checking if the activeBeams[player] has 4 beams inside it and if so then just continue or you can just call the function cleanupBeams without checking i do not understand why you are checking if there is 4 beams in the tool if all the active beams are inside activeBeams[player] table

local tool = script.Parent
local remoteEvent = tool:WaitForChild("RemoteEvent")
local activeBeams = {}

local function cleanupBeams(player)
	if activeBeams[player] then
		for _, obj in pairs(activeBeams[player]) do
			if obj then
				obj:Destroy()
			end
		end
		activeBeams[player] = nil
	end
end

local function onRemoteEventFired(player, action, target)
	if action == "Start" then
		-- Clear any existing beams for the player
		cleanupBeams(player)

		-- Proceed with creating beams only if a valid target exists
		if target and target:FindFirstChild("Humanoid") then
			local character = player.Character
			local head = character and character:FindFirstChild("Head")
			if not head then return end

			-- Fetch LaserStart attachments
			local laserStart1 = head:FindFirstChild("LaserStart")
			local laserStart2 = head:FindFirstChild("LaserStart2")
			if not (laserStart1 and laserStart2) then return end

			-- Create new target attachments
			local targetAttachment1 = Instance.new("Attachment")
			local targetAttachment2 = Instance.new("Attachment")
			targetAttachment1.Parent = target:FindFirstChild("Head") or target:FindFirstChild("Torso")
			targetAttachment2.Parent = target:FindFirstChild("Head") or target:FindFirstChild("Torso")

			-- Use predefined beams from the tool
			local beams = {
				tool:FindFirstChild("Beam1"),
				tool:FindFirstChild("Beam2"),
				tool:FindFirstChild("Beam3"),
				tool:FindFirstChild("Beam4")
			}

			-- Attach beams
			for _, beam in ipairs(beams) do
				if beam then
					beam.Attachment0 = laserStart1
					beam.Attachment1 = targetAttachment1
					beam.Parent = head
				end
			end

			-- Store the active beams and attachments
			activeBeams[player] = {
				targetAttachment1,
				targetAttachment2,
				unpack(beams),
			}

			-- Auto-clean beams after a timeout
			task.delay(5, function()
				cleanupBeams(player)
			end)
		else
			warn("Invalid target!")
		end
	elseif action == "Stop" then
		cleanupBeams(player)
	end
end


remoteEvent.OnServerEvent:Connect(onRemoteEventFired)

Here i made this script its still not working the second time also i dont get any errors anymore

i did not mean doing it like this , i meant using activeBeams[player] to get the active beams