I cant set this value to false?

i have a loop that should turn off when my value becomes false, i try to set it to false but it stays true

local Rec = false

local function InitFolder(Type, Plr, FileName)
	--local Folder = Instance.new("Folder")
	--Folder.Name = "Untitled"..Type
	--Folder.Parent = Plr

	--local VIDDATA = Instance.new("Folder")
	--VIDDATA.Parent = Folder
	--VIDDATA.Name = "VideoData"

	--if Type == "RBV" or Type == "RBV2" then
	--	local SoundFolder = Instance.new("Folder")
	--	SoundFolder.Parent = Folder
	--	SoundFolder.Name = "Sounds"
	--end

	--New

	local Folder = game.ReplicatedStorage:FindFirstChild("Untitled."..Type):Clone()
	Folder.Parent = Plr.Recordings
	Folder.Name = FileName.."."..Type
	return Folder
end

local function Record(plr, FileFormat, FileName)
	if FileFormat == "RBV" then
		Rec = true

		local Location = InitFolder("RBV", plr, FileName)

		print(Rec)
		while Rec == true do
			local FolderClone = game.ReplicatedStorage.TEMP.RBV.Frame:Clone()
			print("looking for: "..FileName.."."..FileFormat)
			FolderClone.Parent = Location
			for _, V in game.Workspace:GetDescendants() do
				if V:IsA("BasePart") or V:IsA("UnionOperation") or V:IsA("MeshPart") or V:IsA("Decal") or V:IsA("Texture") then
					if not V:IsA("Terrain") then
						local ObjectClone = V:Clone()
						ObjectClone.Parent = FolderClone
						--FolderClone.CamPos.Value = game.Workspace.CurrentCamera.CFrame
					end
				end
			end
			task.wait(0.01)
		end


	elseif FileFormat == "RBV2" then
		Rec = true

		local Location = InitFolder("RBV2", plr, FileName)

		while Rec == true do



		end


	elseif FileFormat == "RLV" then
		Rec = true

		local Location = InitFolder("RLV", plr, FileName)

		while Rec == true do



		end


	end
end

game.ReplicatedStorage:WaitForChild("Record").OnServerEvent:Connect(function(plr, P1, P2)
	Record(plr, P1, P2)
	task.wait(5)
	Rec = false
end)
1 Like

Have you tried setting a task.wait inside the

while Rec == true do
-- Add task.wait()
end

Also instead of having multiple while loops, have you tried considering using one? And wrapping each with a coroutine.wrap to not throttle the other functions aswell while keeping your objective which is trying to set it to false.

what is corutine? idk what that is
Sorry im kinda new to coding

Oh, its alright. A coroutine is used to perform multiple tasks at the same time from within the same script.

So for example I can see that you’re trying to make a “Recording” function, while you’ve checked if you’re recording it should be possible that when you’re running multiple record functions it should be able to run simultaneously.

Also if you want it to stop have you tried doing this?

while Rec == true do
    if not Rec then  -- Check if recording should stop
     	break
    end
end

ill try this later, im playing minecraft rn but thanks man

Ok i just tried corutine, it didnt work…

Did i set it up wrong?

game.ReplicatedStorage:WaitForChild("Record").OnServerEvent:Connect(function(plr, P1, P2)
	coroutine.create(Record(plr, P1, P2))
	task.wait(5)
	Rec = false
	print("Stoppped Recording")
end)

I would recommend you use task.spawn().

Replace the line you are currently trying to create a coroutine in with the following code. This will create a new thread with the Record function meaning the code below won’t infinitely yield for the loop which can’t end.

task.spawn(function()
    Record(plr, P1, P2)
end)

That works! thank you mans
chat limit

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