Need help with adding a laser

I am trying to make a laser spawn at the player and have the players max Dmg and Max Length go down.I have multiple scripts.
Global Function

local GlobalFunctions = {}
function GlobalFunctions.shootPistolAdv(player, mouseHit, tool, maxDistance, damage, color)

	local ray = Ray.new(tool.Tip.CFrame.p,(mouseHit.p - tool.Tip.CFrame.p).unit * maxDistance)
	local part, position = workspace:FindPartOnRay(ray, player.Character, false, true)
	local beam = Instance.new("Part", workspace)
	beam.Name = player.Name.."Lazer"
	beam.BrickColor = BrickColor.new(color)
	beam.FormFactor = "Custom"
	beam.Material = "Neon"
	beam.Transparency = 0
	beam.Anchored = true
	beam.Locked = true
	beam.CanCollide = false
	

	local distance = (tool.Tip.CFrame.p - position).magnitude
	if (distance > maxDistance) then
		distance = maxDistance
	end
	beam.Size = Vector3.new(0.5, 0.5, distance)
	beam.CFrame = CFrame.new(tool.Tip.CFrame.p, position) * CFrame.new(0, 0, -distance/2)

	game:GetService("Debris"):AddItem(beam, 600)
	if part then 

		local humanoid = part.Parent:FindFirstChild("Humanoid")
		if not humanoid then
			humanoid = part.Parent.Parent:FindFirstChild("Humanoid")
		end
		if humanoid then
			humanoid:TakeDamage(damage)
		end
	end
end

return GlobalFunctions

local script that adds a new laser into the players backpack and moves the old laser into the players Lazer storage model


local player = game.Players.LocalPlayer

local purchased = false

function onClick()
	if player:FindFirstChild("leaderstats").Points.Value >= 350  and purchased == false then
	
		player:FindFirstChild("leaderstats").Points.Value = player:FindFirstChild("leaderstats").Points.Value - 350
		purchased = true
if player.Backpack:FindFirstChild("Lazer") then
	local lzr = player.Backpack:FindFirstChild("Lazer")  
		
			lzr.Parent = player.LazerStorage
			lzr.BasicLazer.Disabled = true
		local Lzr2 = player.LazerStorage:FindFirstChild("Close Range Lazer")
		Lzr2.Parent = player.Backpack
		print("yay")
		player.leaderstats.Damage.Value = 5
		player.leaderstats.Length.Value = 5
		
		script.Parent.Text = "Processing..."
		wait(3)
		script.Parent.Text = "Purchased!"
		
		elseif player.Character:FindFirstChild("Lazer") then
		local lzr = player.Character:FindFirstChild("Lazer")
			lzr.Parent = player.LazerStorage
			local Lzr2 = player.LazerStorage:FindFirstChild("Close Range Lazer")
			Lzr2.Parent = player.Backpack

			print("yay")
			player.leaderstats.Damage.Value = 5
			player.leaderstats.Length.Value = 5

			script.Parent.Text = "Processing..."
			wait(3)
			script.Parent.Text = "Purchased!"



	end
		end




end
script.Parent.MouseButton1Down:Connect(onClick)

laser shoot script

local canFire = true

local distance = player:WaitForChild("leaderstats"):WaitForChild("Length")

tool.Parent = player.LazerStorage
local dmg = player:WaitForChild("leaderstats"):WaitForChild("Damage")
local maxdmg = 5000
local maxdistance = 20
dmg.Value = dmg.Value
distance.Value = distance.Value

function firePistol(player, mouseHit)
	if canFire == true then
		local maxDistance = distance.Value
		local damage = dmg.Value
		local shootTimer = 1.25
		local color = Color3.fromRGB(41, 225, 0)
		local GlobalFunction = require(game.ServerScriptService.GlobalFunctionsCloseRange)
		local shoot = script.Parent.Parent.Parent.Character.Humanoid:LoadAnimation(script.LazerBlastAnim)
		shoot:Play()
		wait(.1)
		GlobalFunction.shootPistolAdv(player, mouseHit, tool, maxDistance, damage, color)

		dmg.Value = dmg.Value + .5
		distance.Value = distance.Value + .05

		player.leaderstats.Points.Value = player.leaderstats.Points.Value + 2
		canFire = false
		script.Parent.LazerShotSoundAdvanced.Playing = true
		wait(shootTimer)
		script.Parent.LazerShotSoundAdvanced.Playing = false
		canFire = true
	end
end

tool.FireLazer.OnServerEvent:Connect(firePistol)
while true do
	wait(.1)
	if maxdistance <= distance.Value then
		
		distance.Value = maxdistance
	end
	if maxdmg <= dmg.Value then
		dmg.Value = maxdmg
	end
	if script.Parent.HasBeenSeenLength.Value == false and maxdistance <= distance.Value then
		player.PlayerGui.NoMoreLengthGui.Enabled = true
		script.Parent.HasBeenSeenLength.Value = true
	end
	if script.Parent.HasBeenSeenDamage.Value == false and maxdmg <= dmg.Value then
		player.PlayerGui.NoMoreDmgGui.Enabled = true
		script.Parent.HasBeenSeenDamage.Value = true
	end
end

at the bottom of the old laser script there is these lines of code.For some reason the script won’t disable so the bottom of the script keeps running

while true do
	wait(.1)
	if maxdistance <= distance.Value then
		
		distance.Value = maxdistance
	end
	if maxdmg <= dmg.Value then
		dmg.Value = maxdmg
	end
	if script.Parent.HasBeenSeenLength.Value == false and maxdistance <= distance.Value then
		player.PlayerGui.NoMoreLengthGui.Enabled = true
		script.Parent.HasBeenSeenLength.Value = true
	end
	if script.Parent.HasBeenSeenDamage.Value == false and maxdmg <= dmg.Value then
		player.PlayerGui.NoMoreDmgGui.Enabled = true
		script.Parent.HasBeenSeenDamage.Value = true
	end

Yeah. No replies. Waiting… Waiting…

Sorry I’m not sure what the problem is.

ok, I will keep trying to figure it out :slightly_smiling_face: :grinning:

If what you’re trying to do is stop the while true do loop you would use break. It would look some what like this:

while true do
	if script.Parent.HasBeenSeenDamage.Value == false and maxdmg <= dmg.Value then
		player.PlayerGui.NoMoreDmgGui.Enabled = true
		script.Parent.HasBeenSeenDamage.Value = true
                break
	end
end

But once you stop the loop is there a way to start it again?
Also once I buy the next laser it just spawns under a tree, not my hands.

Ten years later. No help.WHAT!!!