How to handle runservice inside module scripts

Hey,

I need help learning on how to handle RunService connections correctly or any connections that delay the script.

Here’s my module

local Framework = {}
Framework.__index = Framework

--[ Services ]--
local RunService = game:GetService("RunService")

function Framework.new()
	local self = setmetatable({},Framework)
	
	return self
end

function Framework:Initilize()
	local Modules = script:GetChildren()
	
	for _,Component in pairs(Modules) do
		print("Initilized: "..Component.Name)

		if Component.Name == "Viewmodel" then
			local Module = require(Component)
			local Viewmodel = Module.new()
			Viewmodel:Equip()
			local function Update(DeltaTime)
				Viewmodel:Update(DeltaTime)
			end
			
			RunService.RenderStepped:Connect(Update) -- this is the problem
		end
		
		if Component.Name == "Movement" then
			local Module = require(Component)
			Module.new()
		end
	end
end


return Framework

When I play the game, the runservice delays the script and other components are not able to be initilized.

try this

function Framework:Initilize()
	local Modules = script:GetChildren()
	
	for _,Component in pairs(Modules) do
		print("Initilized: "..Component.Name)

		if Component.Name == "Viewmodel" then
			local Module = require(Component)
			local Viewmodel = Module.new()
			Viewmodel:Equip()
			
			local function Update(DeltaTime)
				Viewmodel:Update(DeltaTime)
			end
			
			spawn(function()
				RunService.RenderStepped:Connect(Update)
			end)
		end
		
		if Component.Name == "Movement" then
			local Module = require(Component)
			Module.new()
		end
	end
end

So… does it works mate? I’m curious… hehe

1 Like

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