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.