[FIXED] Beam attack bugged (help)

What is the issue?
My problem is that if the beam isn’t intersecting anything, players will only take damage the first time they enter the beam and as long as they aren’t moving.

If the beam isn’t intersecting and the player (target) is moving while in/touching the beam, they still take damage like normal.

What solutions have I tried so far?
I have 2 solutions in mind:

  1. make an instance at the end so the beam will always intersect/hit something
  2. rework the hitbox/damage code (i’m not sure how)

Here’s the code for the beam (not exact, i removed a few things which are unrelated to the problem so you won’t have to add so much to make this work)

local damage = 0.8

if plr.Cooldown7.Value or plr.IsDoingAMove.Value then

	if workspace:FindFirstChild(plr.Name.." [Beam]") then
		plr.IsDoingAMove.Value = false
		workspace:FindFirstChild(plr.Name.." [Beam]"):Destroy()
		plr.Cooldown7.Value = false
	end

else

	plr.Cooldown7.Value = true
	plr.IsDoingAMove.Value = true

	local beam = Instance.new("Part")
	beam.Parent = workspace
	beam.CastShadow = false
	beam.Anchored = true
	beam.CanCollide = false
	beam.Color = Color3.fromRGB(255, 255, 255)
	beam.Material = Enum.Material.Neon
	beam.Transparency = 0.5
	beam.Shape = Enum.PartType.Cylinder
	beam.Massless = true
	beam.Name = plr.Name.." [Beam]"
	beam.Position = plr.Character.Pointer.Position + plr.Character.Pointer.CFrame.LookVector * -6

	local raypara = RaycastParams.new()
	raypara.FilterType = Enum.RaycastFilterType.Exclude
	raypara.FilterDescendantsInstances = {beam, plr.Character}

	local function actuallydobeam() --this function is for repositioning/rotating the beam and applying the damage
		local hithums = {}

		local beamray = workspace:Raycast(plr.Character.Pointer.Position + plr.Character.Pointer.CFrame.LookVector * 5, plr.Character.Pointer.Position + plr.Character.Pointer.CFrame.LookVector * 2051, raypara)
		beam.CFrame = plr.Character.Pointer.CFrame * CFrame.fromOrientation(0, math.rad(90), 0)
		plr.MagicEnergy.Value -= 4

		if beamray and beamray.Distance < 2000 then --if intersection then

			print(beamray.Distance)
			print(beamray.Instance:GetFullName())
			print(beamray.Position)
			print(plr.Character.Pointer.Position)
			beam.Position = plr.Character.Pointer.Position + plr.Character.Pointer.CFrame.LookVector * ((beamray.Distance / 2) + 2.5)
			print(beam.Position)
			beam.Size = Vector3.new(beamray.Distance + 5, 8, 8)--Vector3.new(beamray.Distance + 1, 8, 8)

		else
			beam.Size = Vector3.new(2000, 8, 8) --the fact the beam's size doesn't depend on beamray.Distance might be something...
			local bruhsrsly = beam.CFrame.LookVector * 1000
			print(bruhsrsly)
			beam.Position = plr.Character.Pointer.Position + plr.Character.Pointer.CFrame.LookVector * 1000
		end

		local isitthatframe = true --the isitthatframe part prevents "lingering/overlapping functions" (without this, the beam's damage would be determined based on how long it's active)

		beam.Touched:Connect(function(hit)
			local e = hit.Parent:FindFirstChild("Humanoid")
			local them = game.Players:GetPlayerFromCharacter(hit.Parent)
			if hit.Parent == plr.Character or hithums[e] then return end
			if isitthatframe then
				if e then
					e.Health-= damage
				end
			end
		end)

		task.wait(1/25)
		isitthatframe = false

	end

	repeat
		actuallydobeam()
	until plr.MagicEnergy.Value < 1 or plr.IsDoingAMove.Value == false or plr.Character.Humanoid.Health == 0

	beam:Destroy()
	plr.IsDoingAMove.Value = false
	plr.Cooldown7.Value = false

end

Side notes/other stuff you need:

  • the code above needs to be in a remoteevent
  • there needs to be instances called Cooldown7 (BoolValue), IsDoingAMove (BoolValue), and MagicEnergy (NumValue) parented to the player (not character model). these instances are created by the server.
  • there is an instance called Pointer that’s parented to the player’s character model. it’s controlled by the mouse. these instances are created by the server.
  • the beam doesn’t work that well when you’re on the ground, so you should maybe anchor one player in the air (firing the attack) and the other can roam free on the ground (receiving damage)

Here’s the code I used for the player’s pointer:

local plr = game.Players.LocalPlayer
--the pointer is created by the server
game:GetService("RunService").RenderStepped:Connect(function() --control the block pointer
	local pointer = plr.Character.Pointer
	pointer.CFrame = CFrame.new(workspace.CurrentCamera.CFrame.Position,workspace.CurrentCamera.CFrame.Position+plr:GetMouse().Hit.LookVector)
	pointer.Position = plr.Character.Head.Position
end)

What method should I use to fix it? Should I rework the hitbox/damage part? Should I add an instance that always intersects with the beam? Should I just not fix the bug since players are almost never gonna stay still in the air while flying? (my game has flying combat btw)

UPDATE/EDIT: managed to fix it :partying_face:

3 Likes

If you manage to fix it yourself, please post the solution as a reply to your original post and mark it as the solution, to help anyone else with the issue!

(I’m not necroposting - the original post was edited the same day as I am sending this reply.)

Reply to your own post with the solution, then mark your reply as the solution!

1 Like

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