How to run code in a module script

I agree it does run against the principle of a ModuleScript, however I think it’s best to try and help regardless

1 Like

okay so I was able to get a script running client sided code using @HugeCoolboy2007 's runcontext. The issue is that

------------------------------------------------------------
-- This script handles lobby module functionality on the client.
-- Written by AverageRobloxDev2. All rights reserved.
------------------------------------------------------------


---------------
--// Variables
---------------
local module = require(script.Parent)




---------------
--// Code
---------------
module:run()

is that run is not a valid method of module as proposed by @silver4804

image

1 Like

You’re best bet is to have the module return a function which can return a table while running code

I just want the code inside of the module to run like a normal script on start time. So however I call the module I need to do so to activate the code on runtime

You’d need a regular script/localscript for this

1 Like

just saying that i usually see that type of stuff go into replicatedStorage, which also fixes your previous problem of your localScript not running on workspace

local module = {}

function module.run()
    print("Hello World")
end

return module 

changed the : to ., pretty sure it should work

i’m not sure why you would want a modulescript to run like a normal script when serverscripts/localscripts are there. it’s called modulescripts for a reason. to store multiple functions that can be used on different scripts… and data

I transferred the code that runs on start to a script with runcontext:

------------------------------------------------------------
-- This script handles XYZ functionality on the client.
-- Written by AverageRobloxDev2. All rights reserved.
------------------------------------------------------------


---------------
--// Variables
---------------
local lobbyModule = require(script.Parent)
local UIS = game.UserInputService
local plr = game.Players.LocalPlayer
local mouse = plr:GetMouse()
local ts = game:GetService("TweenService")


---------------
--// Functions
---------------
local function touch()

	-- Detect when they hold down for half a second
	-- Start making the gui follow their finger, relative to the finger position
	-- If they move enough, automatically slide and cancel following the finger
	local index = lobbyModule .getIndex()
	local locked = lobbyModule .getLocked()
	local dragging = false

	if UIS.TouchEnabled then

		UIS.InputBegan:Connect(function(input,gpe)
			if input.UserInputType == Enum.UserInputType.MouseButton1 and locked == false then
				dragging = true
				local counter = 0
				repeat task.wait(.1) counter += .1 until counter == 0.2 or dragging == false
				if dragging then

					locked = true
					local originalPosX = mouse.X/mouse.ViewSizeX

					while dragging do
						task.wait(.1)
						local mousePosX = mouse.X/mouse.ViewSizeX
						local diff = originalPosX - mousePosX 
						if diff >= .20  then
							dragging = false
							index += index
						elseif diff <= -.20 then
							dragging = false
							index -= index
						else
							local guiPos = UDim2.new(1-index, 0, 1, 0)
							local mainMenu = plr.PlayerGui:FindFirstChild("Lobby").lobby.lobbies.Frame
							local tween = ts:Create(mainMenu, TweenInfo.new(.01, Enum.EasingStyle.Linear), {["Position"] = UDim2.new(guiPos+diff, 0, 1, 0)})
							tween:Play()
							tween.Completed:Wait()
						end
					end

					lobbyModule .menu(index)
					locked = false

				end
			end
		end)

		UIS.InputEnded:Connect(function(input,gpe)
			if input.UserInputType == Enum.UserInputType.MouseButton1 and locked == false then
				dragging = false
			end
		end)

	end
end

local function tab(pos, neg)
	UIS.InputBegan:Connect(function(input,gpe)
		
		local index = lobbyModule .getIndex()
		
		if input.keytype == pos then
			lobbyModule .menu(index+1)
		elseif input.keytype == neg then
			lobbyModule .menu(index-1)
		end
	end)
end
---------------
--// Code
---------------
local isTouch  = UIS.TouchEnabled
local isMouse = UIS.MouseEnabled
local isController = UIS.GamepadEnabled 
local isVR = UIS.VREnabled


if isTouch then
	touch()
elseif isMouse then
	print("mosue")
	tab(Enum.KeyCode.Left, Enum.KeyCode.Right)
elseif isController or isVR then
	tab(Enum.KeyCode.ButtonL1, Enum.KeyCode.ButtonR1)
end

No way you put your mark on a script (basically text) that can remove your name

i dont think you have ever taken a computer science class in your life or learned any copyright knowledge

Are you flexing your CompScience class? Most programmers don’t even have that and they’re considered greats. Also, you’d normally put your mark on something that’ll get distributed to others (normally free). But in this context, it’s just text and to make it worse it’s supposed to be an example. And not to roast you or anything but your code looks similar to something i wrote in my first month while I saw self learning to code. I guess those Comp science skills aren’t coming in handy

scripts now have a runcontext property, set that to client rather than server

you were the one who started making fun of me putting a copyright notice in my code. I dont know why you are starting devforum beef bro go to sleep

I wasn’t necessarily making fun of you (sorry in advance if I did) but it’s just something I’ve never seen on thr forums… Ever. So I just overreacted a little bit but im not trying to start online beef :skull:

I didnt manually type it. I have a plugin which automatically formats my scripts like that so its not like I put effort into making it copyrighted or something.

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