I can't exit a function

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    Create a model and zone that will either auto delete after a certain period or can get interrupted and deleted at the player’s command.

  2. What is the issue? Include screenshots / videos if possible!
    When I try to return, my function keeps going and autodeleting. I return to stop the autodeletion process.

  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I tried boolean values but they didn’t seem to work for me

After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!

local function a(plr)

		local char = plr.Character
		local humanoid = char.Humanoid
		local dazone
	local humanoidRootPart = char.HumanoidRootPart
		if not game.Workspace:FindFirstChild(plr.Name.."Room") then
-- Creating the room

		local model = plr.PlayerGui.Room:Clone()

		model.Name = plr.Name .. "Room"
		model.Parent = workspace
		print("createroom")
		model.PrimaryPart.CFrame = humanoidRootPart.CFrame * CFrame.new(Vector3.new(0,-0.5,0))


		local ReplicatedStorage = game.ReplicatedStorage
		local Zone = require(ReplicatedStorage.Zone)
		local Zonecontroller = require(ReplicatedStorage.Zone.ZoneController)
	
		dazone = Zone.fromRegion(model.PrimaryPart.CFrame, model.PrimaryPart.Size)

		for i,v in pairs(dazone.zoneParts) do
			v.Parent.Name = plr.Name .. "Zone"
		end

		dazone.playerEntered:Connect(function(playerwhoentered)
			local bool = playerwhoentered:FindFirstChild("InRoom")
			bool.Value = true
		end)
		
		dazone.playerExited:Connect(function(playerwhoentered)
			local bool = playerwhoentered:FindFirstChild("InRoom")
			bool.Value = false	
	end)
	else	
-- This is the part of the code where you interrupt it
		print("delete")
			game.Workspace:FindFirstChild(plr.Name.."Room"):Destroy()
		local bool = plr:FindFirstChild("InRoom")
		if game.ServerStorage.ZonePlusWorldModel ~= nil then
			
			for i,v in pairs(game.ServerStorage.ZonePlusWorldModel:FindFirstChild(plr.Name .. "Zone").Part:GetTouchingParts()) do
				if v.Parent.Humanoid then
					v.Parent:GetPlayerFromCharacter(v).InRoom.Value = false
				end print(v.Name)
			end
			game.ServerStorage.ZonePlusWorldModel:FindFirstChild(plr.Name .. "Zone"):Destroy()
		end
-- This is the place where it's supposed to interrupt the function from continuing
		return
	end
	
-- Autodelete
		wait(10)	
	print("delete1")
	if game.Workspace:FindFirstChild(plr.Name.."Room") ~= nil then
	game.Workspace:FindFirstChild(plr.Name.."Room"):Destroy()
	local bool = plr:FindFirstChild("InRoom")
		for i,v in pairs(game.ServerStorage.ZonePlusWorldModel:FindFirstChild(plr.Name .. "Zone").Part:GetTouchingParts()) do
			if v.Parent.Humanoid then
		v.Parent:GetPlayerFromCharacter(v).InRoom.Value = false
		end
	end
		game.ServerStorage.ZonePlusWorldModel:FindFirstChild(plr.Name .. "Zone"):Destroy()
			end
		
		
end

 game.ReplicatedStorage.RemoteEvents.CreateRoom.OnServerEvent:Connect(a)

Please do not ask people to write entire scripts or design entire systems for you. If you can’t answer the three questions above, you should probably pick a different category.

To stop a function from running, simply use break.

Example:

for i,v in pairs(workspace:GetChildren()) do
       if v.Name == 'Baseplate' then
             print('found baseplate')
             break
       end
end
1 Like

I think it’s important to mention that that’s a loop, not a function.

But yes, the OP’s problem is that they don’t break out of the loop.

1 Like

Do you need to exit a for loop? And also which loop did I not break out of?

You don’t “need” to, but it’s a good practice if you found, or got the desired result. For their example, their loop found “Baseplate”, so it’s better to break the loop, rather than just letting it go on, looping through everything else.

So if you can, just go through your code, and for every loop, if you found the “item” needed in it (if it’s oriented to find an object in a table), then add an if statement that checks if the item is equal to whatever, and if it is, break the loop.

1 Like

You can keep a table where you index a boolean to the player’s UserId/room name to control if the room should be automatically deleted, for the autodelete script you can delay/wait a certein amount of time and check on the table if the room can be deleted.

1 Like

I just did that, but it didn’t change anything.

	for i,v in pairs(game.ServerStorage.ZonePlusWorldModel:FindFirstChild(plr.Name .. "Zone").Part:GetTouchingParts()) do
			if v.Parent.Humanoid then
		v.Parent:GetPlayerFromCharacter(v).InRoom.Value = false
			end
			if not game.Workspace:FindFirstChild(plr.Name.."Room") then
				break
			end
	end

I did that except the problem is that the player will change the boolean value when they spawn another room.

Since players can spawn multiple rooms you can index the boolean to the room’s name instead, this way when the player spawns another room it wont affect the previous one. You just need to make sure each room has a unique name, mixing the player’s UserId with a number that goes up for each player when a room is spawned should be fine and it would look something like this : “00000000-00”. You can make this number acount for all players’ rooms but having the player name/UserId has a better feedback effect.

1 Like

How am I supposed to index a boolean value to the name?

Here is a example of what i mean, the bool value should be changed by another function if the room no longer need to be autodeleted :

local to_Delete = {}
local players_Nums = {}
local room_Lifetime = 2

local function room_Creation(plr)
	-- [The creating here]
	
	local key = plr.UserId
	players_Nums[key] += 1
	
	local room_Name = plr.UserId.."-"..players_Nums[key]
	to_Delete[room_Name] = true
	
	-- [Names room here]
	
	task.wait(room_Lifetime)
	
	if to_Delete[room_Name] then
		-- [Deletes room here]
	end
end

Notice that for the counter to work it needs to be setted when the player is added to the server.

It’s not going to change anything, all it will do is stop the loop from looping again and again once you found your object.

It will stop it when the object is destroyed.

What I meant was whatever the condition is where your break is in, once it is true, it will exit the loop.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.