This loop wont break in time, any alternative ideas?

Hello this bit of code is suppose to detect the distance between the Mouse.Hit, and the player, and detect if the targeted position is within a certain distance when held down. For context: its to activate a server module, that makes you teleport. So when you hold down, the mouse icon changes to either red or green, if green than you are in the clear and it will activate the tp when you release. If red than it doesn’t work.

For clarification, this code works exactly as intended except for one thing. I have it detecting the new mouse.hit every time the mouse moves in the button1down. And it disconnects the function when you release as to try and avoid it from detecting a nil mouse.hit value. Often it doesnt disconnect imediately and it breaks the script becuase it can’t find a value for the magnitude. I have even tried stopping the mouse when you release so that the mouse move doesn’t trigger before it disconnects. It works better, but the bug still gets through sometimes somehow.

heres the code:

elseif Mode == "Range_Click_Cast" then
	local Mouse_Down = false
	local Mag_Correct = false
	local Clicked = mouse.Button1Down:Connect(function()
		local Spellinlist = player.SpellData:FindFirstChild(CurrentSpell)
		local SpellMastery = player.SpellData:FindFirstChild(CurrentSpell..' Mastery')
		if Mode ~="None" and Spellinlist.Value == true and Energy.Value >= Cost and MagicValues.MagicEnabled.Value == true and Power.Value >= SpellLevels[CurrentSpell] and not settings.CastingCoolOff and settings.Transformed == false and settings.SpellHeld == false then
			Mouse_Down = true
			mouse_Magnitude_Get = mouse.Move:Connect(function()
				if Mouse_Down == true then 
					if mouse.Hit.Position ~= nil then
						local Magnitude = (player.Character.HumanoidRootPart.Position - mouse.Hit.Position).Magnitude
						print(Magnitude)
						if  Magnitude <= settings[CurrentSpell..'_Distance']/SpellMastery.Value then
							Mag_Correct = true
							mouse.Icon = "rbxassetid://3763438308"
						else
							Mag_Correct = false
							mouse.Icon = "rbxassetid://3763432378"
						end
					else
						Mag_Correct = false
					end
				end
			end)
		end
	end)
	
	mouse.Button1Up:Connect(function()
		local Spellinlist = player.SpellData:FindFirstChild(CurrentSpell)
		if Mode ~="None" and Spellinlist.Value == true and Energy.Value >= Cost and MagicValues.MagicEnabled.Value == true and Power.Value >= SpellLevels[CurrentSpell] and not settings.CastingCoolOff and settings.Transformed == false and settings.SpellHeld == false then
			uis.MouseBehavior = Enum.MouseBehavior.LockCurrentPosition
			mouse_Magnitude_Get:Disconnect()
			Clicked:Disconnect()	
			
			mouse.Icon = "rbxassetid://3763409266"
			
			if Mag_Correct == true then
				settings.CastingCoolOff = true
				GUI_CONTROLLER:Progress(Exp)
				self.Services.MagicService.FireSpell:Fire(CurrentSpell,Druid, Sorcerer, Psychic, Imagineet, Necromancer,Output,Exp,settings.OrloviumLight)
				
				wait(1)
				settings.CastingCoolOff = false
			end
			Mag_Correct = false
			CurrentSpell = "None"
			Mode = "None"	
			wait(.5)
			uis.MouseBehavior = Enum.MouseBehavior.Default
		elseif Energy.Value <= Cost then
			self.Controllers.GUI_CONTROLLER:EnergyFail()
		end
	end)

Any ideas? What am i doing in this that i could do better.

You might wish to simplify the first if line in the button1up so you can confirm at run time that all the values you are testing have values you expect.
Split it and add print for each part.

I mean I only really skimmed the code but,

Couldn’t you just check with if Mouse.Hit then?

Instead of using Mouse1ButtonDown and Mouse1ButtonUp maybe just combine them into Mouse1ButtonClick?

Is mouse button 1 click an event for the mouse? i have looked for that but found nothing.

My problem is that it doesn’t disconnect the loop before the mouse.hit doesn’t exist anymore, so it errors trying to create a magnitude with a nil value.

Mouse1ButtonClick should exist, let me look at your script again.

Edit: I’m not home right now, remind me in an hour and 30 mins

1 Like

Pretty sure MouseButton1Clicked only works on Guis and Click Detectors, correct me if i’m wrong though.

By the way, you can end the loop prematurely by adding a break with parameters.

1 Like

Set a variable to mouse.Hit.Position and index it only once per mouse click, that way you don’t have to index it’s position multiple times. Therefore it it goes to nil you still have the value

it wont let me break a mouse.move:connect() for some reason.

This is because it’s firing right after you break it.

A better practise would to be create a function and then use connect on that, makes the code cleaner and easier to understand

function x()
   print("hi")
end

part.Touched:Connect(x) --connecting to an external function