Laser beam 'damage suddenly increases' problem

What do you want to achieve? - Prevent laser from increasing the damage amount

What’s the issue? - Whenever I fire a laser beam, it damages the player as it should the first time, but then slowly when I keep on firing it, it increases the damage done to the player each time I fire it.

Here is the client script:


if input.UserInputType == Enum.UserInputType.E then
local laserAnim = hum:LoadAnimation(animations.laser)
				laserAnim:Play(.3,1,1)
				
				laserStartEvent:FireServer() -- Creates the laser
				
				laserConnection = runService.Heartbeat:Connect(function()
					laserLoopEvent:FireServer(mouse.Hit.Position) -- To update the mouse for the laser beam
				end)

				delay(4, function() -- The laser stays on for 4 seconds and then stops
					laserConnection:Disconnect() -- Stops the run service
					laserStopEvent:FireServer()
					laserAnim:Stop(.3,1,1)
				end)
end

Here is the server:


local beam, laserEffect, A0, A1
laserStartEvent.OnServerEvent:Connect(function(plr)
	local char = plr.Character
	local rootPart = char:WaitForChild("HumanoidRootPart")
	local leftArm = char:WaitForChild("Left Arm")

	laserEffect = misc:WaitForChild("laserEffect"):Clone()
	laserEffect.Parent = workspace.Terrain

	beam = Instance.new("Beam")
	beam.FaceCamera = true
	beam.LightEmission = 1
	beam.LightInfluence = 1
	beam.Texture = "rbxassetid://9402270773"
	beam.TextureLength = 2
	beam.TextureMode = Enum.TextureMode.Stretch
	beam.TextureSpeed = 100
	beam.Transparency = NumberSequence.new(0)
	beam.Width0 = 0.5
	beam.Width1 = 1.25

	A0 = Instance.new("Attachment")
	A1 = Instance.new("Attachment")
	beam.Attachment0 = A0
	beam.Attachment1 = A1

	beam.Parent = workspace.Terrain
	A0.Parent = leftArm
	A1.Parent = workspace.Terrain
	
	local vChance = math.random(1, 2)
	laserLoopEvent.OnServerEvent:Connect(function(x, mousePos)
		pcall(function()
			laserEffect.Position = A1.Position
			
			local rayOriginPos = leftArm.Position
			local rayDirection = (mousePos - rayOriginPos).Unit*750

			local rayParams = RaycastParams.new()
			rayParams.FilterDescendantsInstances = {char, laserEffect}
			rayParams.FilterType = Enum.RaycastFilterType.Blacklist
			
			local rayResult = workspace:Raycast(rayOriginPos, rayDirection, rayParams)
			
			if rayResult and rayResult.Instance then
				local rayResultPos = rayResult.Position
				A1.Position = rayResultPos
			else
				A1.Position = mousePos
			end
			
			if rayResult.Instance.Parent:FindFirstChild("Humanoid") then
				local enemyHum = rayResult.Instance.Parent:FindFirstChild("Humanoid")
				local enemyRootPart = enemyHum.Parent:FindFirstChild("HumanoidRootPart")
				enemyHum:TakeDamage(data.dmg2)
				enemyHum.Sit = true

				if vChance == 1 then
					local flingVel = Instance.new("BodyVelocity", enemyRootPart)
					flingVel.MaxForce = Vector3.new(1,1,1) * 50000000
					flingVel.P = 1000
					flingVel.Velocity = rootPart.CFrame.LookVector*data.velAmount2
					game.Debris:AddItem(flingVel, .01)

					local angVel = Instance.new("BodyAngularVelocity", enemyRootPart)
					angVel.MaxTorque = Vector3.new(1,1,1) * 5000000
					angVel.P = 750
					angVel.AngularVelocity = rootPart.CFrame.LookVector*data.angVelAmount2
					game.Debris:AddItem(angVel, .01)
				elseif vChance == 2 then
					local flingVel = Instance.new("BodyVelocity", enemyRootPart)
					flingVel.MaxForce = Vector3.new(1,1,1) * 50000000
					flingVel.P = 1000
					flingVel.Velocity = rootPart.CFrame.LookVector*data.velAmount2 +  enemyRootPart.CFrame.UpVector*data.upVelAmount2
					game.Debris:AddItem(flingVel, .02)

					local angVel = Instance.new("BodyAngularVelocity", enemyRootPart)
					angVel.MaxTorque = Vector3.new(1,1,1) * 5000000
					angVel.P = 750
					angVel.AngularVelocity = rootPart.CFrame.LookVector*data.angVelAmount2
					game.Debris:AddItem(angVel, .01)
				end
			end
		end)
	end)
end)

laserStopEvent.OnServerEvent:Connect(function(plr)
	if beam and A0 and A1 and laserEffect then
		beam:Destroy(); A0:Destroy(); A1:Destroy(); laserEffect:Destroy()
	end
	
	laserStopEvent:FireClient(plr)
end)

Would appreciate it if someone can solve this problem, as it’s causing instant kills after the damage gets high during the repeated firing of the laser.

This event will be stacked each time you fire the laser.
It never gets disconnected and you won’t see it error (because of pcall) when the attachments and laserEffect are destroyed. But then you create them under the same variables (beam, A0, A1, and laserEffect), so it stops erroring and you have 2 connected functions instead of 1. And then 3 connected functions the next time, and so on.

Not sure what you mean, but you’re saying that the laser loop event never gets disconnected when it stops?

This event connects a function when laserStartEvent is fired.
It is never disconnected, and so when beam, laserEffect, A0, and A1 are destroyed, the function won’t do anything but continues to fire.
Then when laserStartEvent is fired again, it connects a new function but the old function is still connected.
Since beam, laserEffect, A0, and A1 are global variables, when it sets those variables to the newly created objects, the old function will use the new variables.
That’s why it’s stacking.

1 Like