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

How to get module to return number?

ServerScript

local gamemodeNames = {"Deathmatch"}

local replicatedStorage = game:GetService("ReplicatedStorage")
local serverScriptService = game:GetService("ServerScriptService")

local gamemodesFolder = serverScriptService:WaitForChild("GamemodesFolder")

local deathmatchRound = require(gamemodesFolder:WaitForChild("Deathmatch"))

local gamemodes = {deathmatchRound}
local randomSlot = math.random(1, #gamemodes)
local randomGamemode = gamemodes[randomSlot]
gamemode = gamemodeNames[randomSlot]	

wait(gamemode)

ModuleScript

function deathmatch(localTimer)
	localTimer = 100
end

return deathmatch

I'm trying to get the ServerScript to wait a set time, and the set time is set in the module script. I can't just go wait(100) in the main script, cause I plan on having multiple game modes with different times. With what I have now, it just prints a table.

I'm not sure if I need to change what the wait is, or something in the module script to return the number. Please help.

1 answer

✓ Accepted answer

Your current code only returns a function and this function does not return any value:-

function deathmatch(localTimer)
    local Timer = 100
end

return deathmatch

It would probably be best to use a table to store the times ie 'name', time. So we can do something like this:-

-- this is our shared table it will be the same for all scripts
local timeTable = {
	Deathmatch = 100, 
	Deathmatch2 = 200
}

-- this is our accessor method which only returns the value in the table
return function(gameName)
	return timeTable[gameName] -- we simply return the time or nil if it is not found
end

We can then use this module function returned:-

local getTime = require([path to module]) -- the returned accessor function

print(getTime('Deathmatch'))
print(getTime('Deathmatch2'))

I hope this helps.

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