Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How To Destroy This Beam After Magnitude is Higher?

alright so this script here works fine with detecting the players magnitude and creating the beam but for some reason it will not Destroy() the beam

LocalScript:

while wait() do
	for i,v in pairs(game.Workspace.Beams:GetChildren()) do
		local mag = (v.Position - game.Players.LocalPlayer.Character.HumanoidRootPart.Position).magnitude
		if mag <= 12 then
			if v.Ready.Value == true then
				if game.Players.LocalPlayer.Character:FindFirstChild("LookinBeam") == nil then
				local beam = script.Beam:Clone()
				beam.Name = "LookinBeam"
				beam.Parent = game.Players.LocalPlayer.Character
				beam.Attachment0 = v.Attachment
				beam.Attachment1 = game.Players.LocalPlayer.Character.HumanoidRootPart.RootRigAttachment
				end
				if mag >=13 and game.Players.LocalPlayer.Character:FindFirstChild("LookinBeam") then
					game.Players.LocalPlayer.Character.LookinBeam:Destroy()
				end
			end
		end
	end
end

1 answer

✓ Accepted answer

Okay so I think i found the issue, but i can't be 100% sure since I can't reproduce Your exact circumstances. I took the liberty of tidying your code a bit, only after that, I have found the issue.

--I added this line for clarity, should also improve performance a bit
local player = game:GetService("Players").LocalPlayer
local character
local root 
while wait() do

	character = player.Character or player.CharacterAdded:Wait()
	root = character.HumanoidRootPart

	for i,v in pairs(game.Workspace.Beams:GetChildren()) do
	
		local mag = (v.Position - root.Position).magnitude
		if mag <= 12 then
			if v.Ready.Value == true then
				if character:FindFirstChild("LookinBeam") == nil then
					local beam = script.Beam:Clone()
					beam.Name = "LookinBeam"
					beam.Attachment0 = v.Attachment
					beam.Attachment1 = root.RootRigAttachment
					beam.Parent = character --always parent parts last
				end
			end
			--below if statement is inside mag<=12 statement, you need to move one "end" around
		end --end should be here

		if mag >=13 and character:FindFirstChild("LookinBeam") then
			character.LookinBeam:Destroy()
		end
		-- end -- this end has been moved
	end
end

So guys - remove clutter a bit, make proper spaces for statements, etc. Then you can find errors easily. Have a nice scripting session.

2 comments

0

lol wow big issue but a simple fix thanks man lol!

0

No problem :)

This discussion is preserved as it was. New answers, comments, and votes are closed.