Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Can you get the type of a script calling a ModuleScript? [god can lief just]

So lets say I wanted to create a ModuleScript to return a table of variables for services (and potentially other things) and publish it onto the site (which I do indeed want to do):

-- izza called "Services!" :P
local _S = {}
function _S:new(self, v, s) -- this is just here, idk
	self.v = game:GetService(s)
end
do -- this is just to help compile it in the scripting editor
	_S.CollectionService = game:GetService("CollectionService")
	_S.Players = game:GetService("Players")
	_S.GetPlayers = _S.Players:GetPlayers()
	_S.ReplicatedStorage = game:GetService("ReplicatedStorage")
	_S.PhysicsService = game:GetService("PhysicsService")
	_S.RunService = game:GetService("RunService")
	_S.HeartbeatWait = game:GetService("RunService").Heartbeat:Wait()
end

return _S

You might notice that there is no _S.RenderSteppedWait.

That's because:

#RenderStepped can only be used with LocalScripts

But what if I wanted that?

_S.RenderSteppedWait = _S.RunService.RenderStepped:Wait()

But I can only do that with LocalScripts. And it's not like getfenv(2).className is gonna help me here cuz ayy guess wat

##DAT'S TARGETING A VARIABUL

How am I supposed to tell if the script is a LocalScript or a regular script so that the module script knows whether or not to pop in _S.RenderSteppedWait?

RESPONSE:

Weird....

game:GetService("RunService"):IsClient() seems to always be returning true...

This is the code I'm using to test:

-- The ModuleScript, a.k.a. game.Workspace.LeModule
local RunService = game:GetService("RunService")
local function thing() -- test #1
	local OtherScriptName = getfenv(2)["script"].Name
	if getfenv(2).script.ClassName == "LocalScript" then
		print("LeModule was just called by the LocalScript "..OtherScriptName..", running from player "..tostring(RunService:IsClient()).."!")
	else
		print("LeModule was just called by the server script "..OtherScriptName.."!")
	end
end
local _S = {}
if game:GetService("RunService"):IsClient() == true then
	_S.test = "LocalScript"
else
	_S.test = "Script"
end
return _S
-- The ServerScript for testing
workspace:WaitForChild("sweetkid01")
wait(4)
print("Server script calling...")
local thing = require(workspace.LeModule)
print(thing.test)
print("Ding!")
-- The LocalScript, a.k.a. game.StarterPlayer.StarterCharacterScripts.LocalScript. btw testing game is not filtering enabled
wait(2)
print("LocalScript calling...")
local thing = require(workspace.LeModule)
print(thing.test)
print("DingL!")

Output:

--[[
	--[=[ WITH THE TABLE --]=]
	> LocalScript calling...
	> LocalScript
	> DingL!
	> Server script calling...
	> LocalScript
	> Ding!
	--[=[ WITH THE FUNCTION (the scripts are re-formatted for this --]=]	
	> LocalScript calling...
	> LeModule was just called by the LocalScript LocalScript, running from player true!
	> DingL!
	> Server script calling...
	> LeModule was just called by the server script Script!
	> Ding!
--]]

Why does dis happen? Is it because I'm in Studio?

edit: removed edits

edit[2]

Weird... :IsSever() also only returns "Script"...

#edit[3]

ok then i can't believe i didn't tried le working method

#edit[4]

will it work?

uh oh

kmn

######If this post doesn't meet any specific guideline, or you're just simply going to downvote, please comment why you're going to downvote before doing so so that I can adjust this question to be more appropriate.

8 comments

0

Sorry it's taking me so long, I'm testing an answer and I clearly kerfuffled with it.

0

ok clearly i did not kerfuffled clearly it is kerfloofled

0

hello?

0

you could check if game.Players.LocalPlayer is nil or not.

0

err... but then I can't manipulate the table d00d

0

nvm

0

mentally retarded is me

0

double mental retardationationationationationationatioantiaontaointoiatiogdknjhgbmjfd hsKLmd

3 answers

If your module was called by a LocalScript, when you use

game:GetService("RunService"):IsClient()

it should return the player from which the Module is required. If it is called from a regular Script object, it would just return nil.

I believe getfenv(2).script.ClassName should work if you put it inside the new() function.

But if that doesn't work, you can just use normal Stepped which can be used in both server and client scripts.

You can use: game:GetService('RunService'):IsClient() or check if 'LocalPlayer' exists

This discussion is preserved as it was. New answers, comments, and votes are closed.