Stopping function from other function

I have something like this in my script (If there are any syntax mistakes in script below it doesnt matter because i dont know it out of my head and in my actual script it is correct).

local UIS = game:GetService("UserInputService")
function test()
      UIS.InputBegan:Connect(function(input)
         if input.UserInputType == Enum.InputType.MouseButton2 then
         --cancel
         end
      end
end

Where i commented “cancel” i would like to return out of test() but if i type “return” in the input function it would just return out of that function and not the test() one. I don’t know how to get a return value from the input function since if i would just put it inside a variable, it would contain the bind between UIS.InputBegan and the function. And even if did something like this:

local UIS = game:GetService("UserInputService")
function test()
      UIS.InputBegan:Connect(function(input)
         if input.UserInputType == Enum.InputType.MouseButton2 then
              return true
         end
      end

      if --[[function above returned true]] then
           return
      end
end

it would still only check for the return value once when the test() function is fired, not when i right click.
So how would you go about returning out of the test() function when i right click? Thanks.

If I’m reading this correctly, you want something like this?

local UIS = game:GetService("UserInputService")
local returnedTrue = false
function test()
      UIS.InputBegan:Connect(function(input)
         if input.UserInputType == Enum.InputType.MouseButton2 then
              returnedTrue = true
              return true
         end
      end

      if returnedTrue then
           return
      end
end

Yes but the problem is that the if statement is inside the test function so it will only check for returnedTrue once for every time the test() function is called. So when i press right click after that one check happens it only fires the inpu function which sets returnedTrue to true. The if statement below will not run tho bc it is outside the function connected to InputBegan. This is what would happen for example:

test() is fired

function inside test() is connected to InputBegan

returnedTrue has not changed because i didnt click yet so if statement doesnt run

I right click

The function connected to InputBegan runs

sets returnedTrue to true

The if statement doesn’t check for returnedTrue because it is inside test but not inside the other function
.

A solution would be to put the if statement inside InputBegan but then that would be inside a function so “return” would exit out of the input function but not out of test().

Atleast i think so i’m not that skilled at scripting but i’ve tried something like that before and it didn’t work. But that could also just be my bad scripting

I would use bindable events and disconnect their connection when I want to. Simple

Good idea, i was wondering how i could use this in my situation though. I know what to do the problem is just that i need to define things in a order for certain things to work but that would break other parts of the script.

This is the script:

local RunService = game:GetService("RunService")
local UIS = game:GetService("UserInputService")
local placeEventServer = game.ReplicatedStorage:WaitForChild("PlaceEvent")

function cancel()
	--unbind bind and bind2
	return
end


function place(localItem)
	local part = localItem:WaitForChild("Part").Value:WaitForChild("model").value
	local ghost = part:Clone()
	ghost.Anchored = true
	ghost.CanCollide = false
	ghost.CanQuery = false	
	ghost.Transparency = .5
	ghost.Color = Color3.new(0, 1, 0)
	ghost.Parent = game.Workspace
	
	bind = RunService.RenderStepped:Connect(function()
		ghost.Position = game.Players.LocalPlayer:GetMouse().Hit.Position + Vector3.new(0,ghost.Size.Y/2,0)
	end)
	
	wait()
	bind2 = UIS.InputBegan:Connect(function(input)
		if input.UserInputType == Enum.UserInputType.MouseButton2 then
			ghost:Destroy()
			cancel()
		elseif input.UserInputType == Enum.UserInputType.MouseButton1 then
			placeEventServer:FireServer(part)
		end
	end)
	
end


script.Parent:WaitForChild("placeEvent").Event:Connect(place)	

The problem is that cancel() would need to disconnect bind and bind2, which it can’t since they haven’t been defined yet. But i can’t put it after defining bind2 because cancel is called from the function in bind2 so then that function can’t call cancel() because it hasn’t been defined. Maybe i could just in the beginning of the script define bind and bind2 as nil and then in the script change it to the connections between the events and the functions later. Not sure that would work i’m going to test it now.

You can put a connection argument to disconnect it, but it would be pointless since you can already do that in one line

Also you could just define bind in the first line and leave it nil, then connect it after a few lines

Thanks i put the :Disconnect() after it and it just didn’t give an error so i suppose it works now. :slight_smile:

1 Like