In need of help with module script

I have just recently found roblox-server-js, and I followed the steps in the tutorial that comes with it, but for some reason it errors. I would assume it’s because I have know idea how to use module script because they have been intimidating to me, but other than that I wouldn’t know the problem. It outputs 11:57:09.035 - ServerScriptService.Server:5: '<eof>' expected near 'local'

Here’s the script:

local module = {

local groups = {} --Redline under "local".
local module = {}
local setmetatable = setmetatable --Clicking the output takes me here.
local error = error
local wait = wait
local httpService = game:GetService'HttpService'
local postAsync = httpService.PostAsync
local getAsync = httpService.GetAsync
local jsonEncode = httpService.JSONEncode
local jsonDecode = httpService.JSONDecode
local base, key

local function encode (tab)
  return jsonEncode(httpService, tab)
end

local function decode (tab)
  return jsonDecode(httpService, tab)
end

local function request (url, method, suppress, data)
  local body = method(httpService, url, data)
  local success, response = pcall(decode, body)
  local err
  if success then
err = response.error
  else
err = 'Response was not valid json, full body: '..body
  end
  if not err and suppress then
return true, response
  elseif not err then
return response
  elseif suppress then
return false, err
  else
error(err)
  end
end

local http = {
  post = function (path, data, suppress)
if not data then
  data = {}
end
data.key = key
return request(base..path, postAsync, suppress, encode(data))
  end,
  get = function (path, suppress)
return request(base..path, getAsync, suppress)
  end
}

function groups.promote (group, userId)
  return http.post('/promote/'..group..'/'..userId)
end

function groups.demote (group, userId)
  return http.post('/demote/'..group..'/'..userId)
end

function groups.setRank (group, userId, rank)
  return http.post('/setRank/'..group..'/'..userId..'/'..rank)
end

function groups.shout (group, message)
  return http.post('/shout/'..group, {message = message})
end

function groups.post (group, message)
  return http.post('/post/'..group, {message = message})
end

function groups.handleJoinRequest (group, username, accept)
  local acceptString = accept and 'true' or 'false'
  return http.post('/handleJoinRequest/'..group..'/'..username..'/'..acceptString)
end

function groups.getPlayers (group, rank, limit, online)
  local job = http.post('/getPlayers/make/'..group..(rank and '/'..rank or '')..'?limit='..(limit and limit or '-2')..'&online='..(online and 'true' or 'false')).data.uid
  local complete, response = false
  repeat
wait(0.5)
local success
success, response = http.get('/getPlayers/retrieve/'..job, true)
if not success and response:match('$Response was not valid json') then
  error(response)
elseif success then
  complete = response.data.complete
end
  until complete
  return response
end

function module.message (userId, subject, message)
  return http.post('/message/'..userId, {subject = subject, body = message})
end

function module.forumPostNew (forumId, subject, body, locked)
  return http.post('/forumPost/new/'..forumId..'?locked='..(locked and 'true' or 'false'), {subject = subject, body = body})
end

function module.forumPostReply (postId, body, locked)
  return http.post('/forumPost/reply/'..postId..'?locked='..(locked and 'true' or 'false'), {body = body})
end

function module.getBlurb (userId)
  return http.get('/getBlurb/'..userId)
end

return function (domain, newKey, group)
  local isString = (type(domain) == 'string')
  if (not domain) or (not isString) or (isString and #domain <= 0) then
error('Url is required and must be a string greater than length 0')
  end
  isString = (type(newKey) == 'string')
  if (not newKey) or (not isString) or (isString and #newKey <= 0) then
error('Key is required and must be a string greater than length 0')
  end

  base = 'http://'..domain
  key = newKey

  if group then
local isNumber = (type(group) == 'number')
if (not isNumber) or (group <= 0) then
  error('If group is provided it must be a number greater than 0')
end

for name, func in next, groups do
  module[name] = function (...)
    return func(group, ...)
  end
end
return module
  end

  for name, func in next, groups do
module[name] = func
  end
  return module
end

}



return module

Yes it’s a long script, but assuming that I am correct it would probably have to do with the area I put it in. Also, I put it in a regular script, but it didn’t error.

1 Like

Your issue probably stems from “setmetatable” and “error” and “wait”

Already it’s kind of redundant, and I don’t think it’s proper syntax.
The reason its saying that it expected an near ‘local’ is because usually setmetatable is done like:

setmetatable(tab, metTab)
1 Like

Yap, as greek said, this is probarly a syntax problem. And by that, I don’t see the need of doing this?

1 Like

The issue is that local variable declarations are in the module table. This is not possible.

The last line before the return statement has } and I’m assuming that’s closing the module table construction.

@GreekForge you don’t need to call a function immediately once you have a reference to it, you can store a reference to it in a variable, but the way OP is doing it is kinda pointless caching all these functions

Here’s a better visual

local a = {
    local b = 8,
    local c = true
}

-- `local b` is not a valid identifier. 

Because there isn’t.

2 Likes

This isn’t my script. It’s a script made by sentanos. I would link you to it, but I don’t know if I’m allowed to do so. It’s supposed to make it so you can promote, demote, post, delete, kick, accept, and e.t.c in groups from a ROBLOX game.

EDIT: This is also connected with JavaScript and stuff.

1 Like

But it’s a public repository.

2 Likes

Yeah that link. I just wasn’t sure if I was allowed to link to websites that aren’t Roblox.

1 Like

In any case, as for the original script and the reason why you’re experiencing errors, doesn’t seem like you copied it properly. The whole script itself is meant to be a ModuleScript. Don’t modify it.

You’re supposed to copy its raw contents, put it into a ModuleScript and require it. Don’t encapsulate the entire script in a table and return it, that’s why you’re getting errors.

The code returns a function that you use. The errors are occurring because you’re trying to modify it with no idea of what edits you’re making.

2 Likes

Okay, thanks. I should’ve copied it how it originally was.