This script continues to run more than once after it is first initiated and I can't figure out how to stop it. I don't understand why once it completes each function it doesn't end like its supposed to. I tried this myself but couldn't figure it out, any ideas? let me know if you need me to clarify anything

local BUTTON = script.Parent
local player = game.Players.LocalPlayer
local CURRENT = nil
local mouse = player:GetMouse()
local RANGE = 15


function do_remove_all(parent)
	local children = parent:GetChildren()

	for index, child in pairs(children) do
		child:remove()
	end
end

function rangeCheck(mouse)
	local char = player.Character
	local torso = char.Torso

	return (torso.Position - mouse.hit.p).magnitude < RANGE
end



function removeExample()
	local player = game.Players.LocalPlayer
	local char = player.Character

	local ex_check = char:findFirstChild("BACKPACK_EXAMPLE")

	if ex_check ~= nil then
		ex_check:remove()
	end
end

function onGuiClick(part)
	CURRENT = part
end


function onMouseMove(mouse)
	local player = game.Players.LocalPlayer
	local char = player.Character
	local torso = char.Torso
	local pack = player:findFirstChild("Pack")
local pack_tab = pack:GetChildren()
local partname = BUTTON.Text
local part = pack:FindFirstChild(partname)

	removeExample()
	
	
		if rangeCheck(mouse) then
			
			local example = part:Clone()
			example.Name = "BACKPACK_EXAMPLE"
			example.Locked = true
			example.Transparency = 0.5
			example.Parent = char
			example.Position = mouse.hit.p
			do_remove_all(example)
			
		end
	end


function onButton1Down(mouse)
	local pack = player:findFirstChild("Pack")
local pack_tab = pack:GetChildren()
local partname = BUTTON.Text
local part = pack:FindFirstChild(partname)
local player = game.Players.LocalPlayer
local char = player.Character
local example = char:FindFirstChild("example")

	if part ~= nil then
		if rangeCheck(mouse) then
			removeExample()
			print("clicked")

			part.Parent = game.Workspace
			part.Locked = false
			part.Anchored = false
			part.Position = mouse.hit.p
			
			part = nil
			
			--	repeat wait() until MouseButton1Down    I'm trying something like this but don't know how to phrase it
	end
	end
	end

function onKeyDown(key, mouse)
	if key == "r" then
		removeExample()
		local CURRENT = nil
	elseif key == "e" then
		local player = game.Players.LocalPlayer
		local pack = player:findFirstChild("Pack")
		local pack_tab = pack:GetChildren()

		for index, child in pairs(pack_tab) do
			local CURRENT = child
			onButton1Down(mouse)
			wait(0.1)
		end

		local CURRENT = nil
	end
end

function onMouseButton1Down()

		print("buttonclicked")
	mouse.Button1Down:connect(function() onButton1Down(mouse) end)
	mouse.Move:connect(function() onMouseMove(mouse) end)
	mouse.KeyDown:connect(function(key) onKeyDown(key, mouse) end)

end

BUTTON.MouseButton1Down:connect(onMouseButton1Down)

2 comments

0

Where is this script placed?

0

its in a button and starts when the button is pressed