Detecting if a function was ran (SERVER SCRIPT)

Hello, I have been wondering how I could possibly detect if a function inside a SERVER SCRIPT has started running WITHOUT VARIABLES.

I have been looking around but all I found was a solution with variables (which is not what I want to use, because then I need to use LOOPS) or questions about module scripts.

example:

local function HandleButtonPress()
       --something
end

spawn(function()
        --something
        --detect when the HandleButtonPress function is run and run its own code
end)
--code
HandleButtonPress()
--/code

I would appreciate any help!

3 Likes

Just run a function when HandleButtonPress() runs.

local function OnButtonPressFunction()
        --Code
end

local function HandleButtonPress()
       --something
       task.spawn(function()
               OnButtonPressFunction()
       end)
       
end

HandleButtonPress()
2 Likes

That would unecessarily overcomplicate things. look closely at my code example, it has alot to do with my current code just without the unecessary details.

1 Like

What are you exactly trying to do though there is probably a better way to do it

1 Like

I want code inside of the spawn function (see example code) to check if the function HandleButtonPress was ran without any help of the outside parts (basically an independent piece of code)

1 Like

Yea but why though if you want some action to be triggered just use the function method that the person before me told you about

1 Like

thats because i need the variables from inside the spawn function.

1 Like

Just put the variables outside the spawn function

1 Like

I need multiple of those variables at a time because the function looks for instances by the name of “_Button”…buttonnum

it will then create a bindable event corresponding to it inside of a folder, so putting all of these variables outside of the spawn function will just extremely overcomplicate things

1 Like

Do you have a solution or a step to the solution for that?

1 Like

I don’t know what kinda weird code you have but just use another bindablevent and fire it when the function is called
just know there are way more clean ways to achieve what you want but you didn’t show any other code

1 Like

that other code is kind of irrelevant but i guess ill show you

local buttonNum = 1
local maxBtnCount = 15
local sound = workspace.SFX.ButtonPress
local buttn = "_Button"
local debounce = false
local bdeb = false
local interactive = {}
local specials = require(script.SpecialActions)
local function MaxButtons()
	if buttonNum > maxBtnCount then
		print("die")
		local humanoid = game.Players:GetPlayers().Character.Humanoid
		humanoid.Health = 0
	end
end

local maps = game.ServerStorage.Maps

local function HandleButtonPress(button)
	if debounce == false then
		--irrelevant code
	else
		return
	end
end
spawn(function()
    --relevant code
	local nown = 1
	repeat
		local bas = workspace:FindFirstChild("_Button"..nown, true)
		if bas then
			local ev = Instance.new("BindableEvent", script.Parent.Bindables)
			ev.Name = tostring("Bindable"..nown)
			nown+=1
			--HandleButtonPress() = function()

			--end
		else
			nown = -1
		end
	until
	nown == -1
end)
for i = 1, maxBtnCount do
	local button = workspace:FindFirstChild(buttn..i, true)
	if button then
		button.Hitbox.Touched:Connect(function(hit)
			if not bdeb then
				bdeb = true
				local p = game.Players:GetPlayerFromCharacter(hit.Parent)
				if p then
					script.Parent.Button:Fire(p, i)
					HandleButtonPress(button)
					button.Hitbox.CanTouch = false
				end
			end
		end)
	end
end

1 Like

I might have not gotten something since this code is horrendous but can’t you just connect to the bindableevent since you are firing it with the function?

1 Like

Sorry for the terrible code xd I made this like a year ago

I just tried the Button function and i think it worked

Just rewrite it at this point and make sure to use a for loop instead of repeat until and use task.spawn instead of spawn

1 Like

I just wrote that code because I didnt wanna take so much time to write it, and it worked without issues so i dont get why i should use for loops.

1 Like

readability + cleanliness + for loops are easier to write
also you are searching through the ENTIRE workspace every time you are searching for a button (workspace:FindFirstChild(“_Button”…nown, true)) put them in a model or a folder
(rewrite this pls)

1 Like

This wont always be a possibility.
You know the game Flood Escape 2?

Not only that but if i dont find all the buttons in one for loop then i will have to repeat it

Ok stop yappin put the dang buttons in a model and that’s it

Tell me 1 single reason why my example wouldn’t work. You want a function to run every time that function runs, so why doesn’t my example work?