local V={RequestDataRepeat=10,SetterError="\091\082\101\112\108\105\099\097\067\111\110\116\114\111\108\108\101\114\093\058 \082\101\112\108\105\099\097 \115\101\116\116\101\114\115 \099\097\110 \111\110\108\121 \098\101 \099\097\108\108\101\100 \105\110\115\105\100\101 \119\114\105\116\101 \102\117\110\099\116\105\111\110\115"}local B do local V=game:GetService("\082\117\110\083\101\114\118\105\099\101")local function I(B,I,b)local K=B:FindFirstChild(I,true)if K==nil then local D=os.clock()local J J=B.DescendantAdded:Connect(function(V)if V.Name==I then K=V end end)while K==nil do if D~=nil and(os.clock()-D>1 and(V:IsServer()==true or game:IsLoaded()==true))then D=nil warn("\091"..(script.Name..("\093\058 \077\105\115\115\105\110\103 "..(b..(" \""..(I..("\" \105\110 "..(B:GetFullName().."\059 \080\108\101\097\115\101 \099\104\101\099\107 \115\101\116\117\112 \100\111\099\117\109\101\110\116\097\116\105\111\110"))))))))end task.wait()end J:Disconnect()return K else return K end end local b if V:IsServer()==true then b=Instance.new("\070\111\108\100\101\114")b.Name="\082\101\112\108\105\099\097\082\101\109\111\116\101\069\118\101\110\116\115"b.Parent=game:GetService("\082\101\112\108\105\099\097\116\101\100\083\116\111\114\097\103\101")else b=I(game:GetService("\082\101\112\108\105\099\097\116\101\100\083\116\111\114\097\103\101"),"\082\101\112\108\105\099\097\082\101\109\111\116\101\069\118\101\110\116\115","\102\111\108\100\101\114")end B={GetShared=function(V,B)return I(script.Parent,B,"\109\111\100\117\108\101")end,GetModule=function(V,B)return I(script.Parent,B,"\109\111\100\117\108\101")end,SetupRemoteEvent=function(B)if V:IsServer()==true then local V=Instance.new("\082\101\109\111\116\101\069\118\101\110\116")V.Name=B V.Parent=b return V else return I(b,B,"\114\101\109\111\116\101 \101\118\101\110\116")end end,Shared={}}local K=require(B.GetShared("\077\097\100\119\111\114\107","\077\097\100\119\111\114\107\083\099\114\105\112\116\083\105\103\110\097\108"))B.NewScriptSignal=K.NewScriptSignal B.NewArrayScriptConnection=K.NewArrayScriptConnection end


local SETTINGS = {
	RequestDataRepeat = 10,
	SetterError = "[ReplicaController]: Replica setters can only be called inside write functions",
}

local Madwork -- Standalone Madwork reference for portable version of ReplicaService/ReplicaController
do
	local RunService = game:GetService("RunService")
	
	local function WaitForDescendant(ancestor, instance_name, warn_name)
		local instance = ancestor:FindFirstChild(instance_name, true) -- Recursive
		if instance == nil then
			local start_time = os.clock()
			local connection
			connection = ancestor.DescendantAdded:Connect(function(descendant)
				if descendant.Name == instance_name then
					instance = descendant
				end
			end)
			while instance == nil do
				if start_time ~= nil and os.clock() - start_time > 1
					and (RunService:IsServer() == true or game:IsLoaded() == true) then
					start_time = nil
					warn("[" .. script.Name .. "]: Missing " .. warn_name .. " \"" .. instance_name
						.. "\" in " .. ancestor:GetFullName() .. "; Please check setup documentation")
				end
				task.wait()
			end
			connection:Disconnect()
			return instance
		else
			return instance
		end
	end
	
	local RemoteEventContainer
	if RunService:IsServer() == true then
		RemoteEventContainer = Instance.new("Folder")
		RemoteEventContainer.Name = "ReplicaRemoteEvents"
		RemoteEventContainer.Parent = game:GetService("ReplicatedStorage")
	else
		RemoteEventContainer = WaitForDescendant(game:GetService("ReplicatedStorage"), "ReplicaRemoteEvents", "folder")
	end
	
	Madwork = {
		GetShared = function(package_name, item_name)
			-- Ignoring package_name as we're working without Madwork framework
			return WaitForDescendant(script.Parent, item_name, "module")
		end,
		GetModule = function(package_name, module_name)
			return WaitForDescendant(script.Parent, module_name, "module")
		end,
		SetupRemoteEvent = function(remote_name)
			if RunService:IsServer() == true then
				local remote_event = Instance.new("RemoteEvent")
				remote_event.Name = remote_name
				remote_event.Parent = RemoteEventContainer
				return remote_event
			else
				return WaitForDescendant(RemoteEventContainer, remote_name, "remote event")
			end
		end,
		Shared = {}, -- A Madwork package reference - ReplicaService will try to check this table
	}
	
	local MadworkScriptSignal = require(Madwork.GetShared("Madwork", "MadworkScriptSignal"))
	Madwork.NewScriptSignal = MadworkScriptSignal.NewScriptSignal
	Madwork.NewArrayScriptConnection = MadworkScriptSignal.NewArrayScriptConnection
end
