Variable Manager

An module that i made just for fun.
Its all about types of variables…

local module = {}
local initm = require(script.Parent.Init)

function module.Variable(any : any)
	local base = {}
	
	base.Value = any
	
	do
		local event : RBXScriptSignal = {}
		
		function event:Connect(func : () -> ())
			local main = func
			local rbxsc = initm.connection(true, function() 
				main = nil
			end)
			spawn(function()
				while wait() do
					if main == nil then break end
					main()
				end
			end)
			return rbxsc
		end
		
		function event:Once(func : () -> ())
			local main = func
			local rbxsc = initm.connection(true, function() 
				main = nil
			end)
			spawn(function()
				main()
			end)
			return rbxsc
		end
		
		function event:ConnectParallel(func : () -> ())
			local main = func
			local rbxsc = initm.connection(true, function() 
				main = nil
			end)
			task.spawn(function()
				while wait() do
					if main == nil then break end
					main()
				end
			end)
			return rbxsc
		end
		
		function event:Wait() : nil
			wait()
		end
		
		base.VariableUpdateEachFrame = event
	end
	
	do
		local event : RBXScriptSignal = {}

		function event:Connect(func : () -> ())
			local main = func
			local rbxsc = initm.connection(true, function() 
				main = nil
			end)
			spawn(function()
				local updater = nil
				updater = base.Value
				while wait() do
					if main == nil then break end
					if updater ~= base.Value then
						updater = base.Value
						main()
					end
				end
			end)
			return rbxsc
		end
		
		function event:Once(func : () -> ())
			local main = func
			local rbxsc = initm.connection(true, function() 
				main = nil
			end)
			spawn(function()
				local updater = nil
				updater = base.Value
				while wait() do
					if main == nil then break end
					if updater ~= base.Value then
						updater = base.Value
						main()
						main = nil
					end
				end
			end)
			return rbxsc
		end
		
		function event:Wait()
			local updater = nil
			updater = base.Value
			while wait() do
				if updater ~= base.Value then
					break
				end
			end
		end
		
		function event:ConnectParallel(func : () -> ())
			local main = func
			local rbxsc = initm.connection(true, function() 
				main = nil
			end)
			task.spawn(function()
				local updater = nil
				updater = base.Value
				while wait() do
					if main == nil then break end
					if updater ~= base.Value then
						updater = base.Value
						main()
					end
				end
			end)
			return rbxsc
		end

		base.Changed = event
	end
	
	export type RBXVariable = typeof(base)
	return base
end

function module.ConstantVariable(any : any)
	local base = {}
	
	base.Value = any
	
	table.freeze(base)
	
	export type RBXConstantVariable = typeof(base)
	return base
end

function module.RangedNumericalVariable(number : number, min : number?, max : number?)
	local base = {}
	
	if not min then min = -1 end; if not max then max = 1 end;
	base.Value = number
	
	task.spawn(function()
		base.Value = math.clamp(base.Value, min, max)
	end)
	
	do
		local event : RBXScriptSignal = {}

		function event:Connect(func : () -> ())
			local main = func
			local rbxsc = initm.connection(true, function() 
				main = nil
			end)
			spawn(function()
				while wait() do
					if main == nil then break end
					main()
				end
			end)
			return rbxsc
		end

		function event:Once(func : () -> ())
			local main = func
			local rbxsc = initm.connection(true, function() 
				main = nil
			end)
			spawn(function()
				main()
			end)
			return rbxsc
		end

		function event:ConnectParallel(func : () -> ())
			local main = func
			local rbxsc = initm.connection(true, function() 
				main = nil
			end)
			task.spawn(function()
				while wait() do
					if main == nil then break end
					main()
				end
			end)
			return rbxsc
		end

		function event:Wait() : nil
			wait()
		end

		base.VariableUpdateEachFrame = event
	end

	do
		local event : RBXScriptSignal = {}

		function event:Connect(func : () -> ())
			local main = func
			local rbxsc = initm.connection(true, function() 
				main = nil
			end)
			spawn(function()
				local updater = nil
				updater = base.Value
				while wait() do
					if main == nil then break end
					if updater ~= base.Value then
						updater = base.Value
						main()
					end
				end
			end)
			return rbxsc
		end

		function event:Once(func : () -> ())
			local main = func
			local rbxsc = initm.connection(true, function() 
				main = nil
			end)
			spawn(function()
				local updater = nil
				updater = base.Value
				while wait() do
					if main == nil then break end
					if updater ~= base.Value then
						updater = base.Value
						main()
						main = nil
					end
				end
			end)
			return rbxsc
		end

		function event:Wait()
			local updater = nil
			updater = base.Value
			while wait() do
				if updater ~= base.Value then
					break
				end
			end
		end

		function event:ConnectParallel(func : () -> ())
			local main = func
			local rbxsc = initm.connection(true, function() 
				main = nil
			end)
			task.spawn(function()
				local updater = nil
				updater = base.Value
				while wait() do
					if main == nil then break end
					if updater ~= base.Value then
						updater = base.Value
						main()
					end
				end
			end)
			return rbxsc
		end

		base.Changed = event
	end
	
	export type RBXRangedVariable = typeof(base)
	return base
end

function module.Int(value : number)
	local base = {}
	
	base.Value = value
	
	task.spawn(function()
		base.Value = math.ceil(base.Value)
	end)
	
	do
		local event : RBXScriptSignal = {}

		function event:Connect(func : () -> ())
			local main = func
			local rbxsc = initm.connection(true, function() 
				main = nil
			end)
			spawn(function()
				while wait() do
					if main == nil then break end
					main()
				end
			end)
			return rbxsc
		end

		function event:Once(func : () -> ())
			local main = func
			local rbxsc = initm.connection(true, function() 
				main = nil
			end)
			spawn(function()
				main()
			end)
			return rbxsc
		end

		function event:ConnectParallel(func : () -> ())
			local main = func
			local rbxsc = initm.connection(true, function() 
				main = nil
			end)
			task.spawn(function()
				while wait() do
					if main == nil then break end
					main()
				end
			end)
			return rbxsc
		end

		function event:Wait() : nil
			wait()
		end

		base.VariableUpdateEachFrame = event
	end

	do
		local event : RBXScriptSignal = {}

		function event:Connect(func : () -> ())
			local main = func
			local rbxsc = initm.connection(true, function() 
				main = nil
			end)
			spawn(function()
				local updater = nil
				updater = base.Value
				while wait() do
					if main == nil then break end
					if updater ~= base.Value then
						updater = base.Value
						main()
					end
				end
			end)
			return rbxsc
		end

		function event:Once(func : () -> ())
			local main = func
			local rbxsc = initm.connection(true, function() 
				main = nil
			end)
			spawn(function()
				local updater = nil
				updater = base.Value
				while wait() do
					if main == nil then break end
					if updater ~= base.Value then
						updater = base.Value
						main()
						main = nil
					end
				end
			end)
			return rbxsc
		end

		function event:Wait()
			local updater = nil
			updater = base.Value
			while wait() do
				if updater ~= base.Value then
					break
				end
			end
		end

		function event:ConnectParallel(func : () -> ())
			local main = func
			local rbxsc = initm.connection(true, function() 
				main = nil
			end)
			task.spawn(function()
				local updater = nil
				updater = base.Value
				while wait() do
					if main == nil then break end
					if updater ~= base.Value then
						updater = base.Value
						main()
					end
				end
			end)
			return rbxsc
		end

		base.Changed = event
	end
	
	export type RBXInt = typeof(base)
	return base
end

export type VariableManagerService = typeof(module)
return module
1 Like