I want to call a function that is inside a module from a string. I'm using loadstring. Nothing happens when calling the module from inside IF/ELSE statement. But it does work when calling it from the functionReturned variable.
This is what i have in ServerScriptService
local lehModule = require(game.ServerScriptService.ModuleScript)
local functionReturned = lehModule.checkCommand(item["Command"],item["MoreInfo"])
if functionReturned then
local lehString = "lehModule"..functionReturned
if item["MoreInfo"] then
local stuff = "(\""..tostring(item["Command"]).."\",\""..tostring(item["MoreInfo"]).."\")"
--prints: lehModule.killPlayer("kill","Player1")
print("lehFunc: "..lehString..stuff)
loadstring(lehString..stuff)
else
--prints: lehModule.killall()
print(lehString.."("..")")
loadstring(lehString.."("..")")
end
end
and this is what i have in my module script:
local module = {}
local commands = {["kill all"]=".killall",["tp"]=".tp",["changeTime"]=".changeTime",["kill"]=".killPlayer",["messageToPlayer"]=".messagePlayer",["messageAll"]=".messageAll"}
function module.messageAll(msg)
end
function module.messagePlayer(msg)
end
function module.killPlayer(plr)
local player = game.Workspace:FindFirstChild(plr)
if player then
player.Humanoid.Health = 0
else
print("inside Kill Player: ",player)
end
end
function module.killall()
for i in pairs(game.Players:GetChildren()) do
print("killAll i: ",i)
local player = game.Workspace:FindFirstChild(i.Name)
if player then
player.Character.Health = 0
else
print("leh I: ",i.Name)
end
end
return "done"
end
function module.tp(info)
local plr1 = ""
local plr2 = ""
local plr1Done = false
for i in string.gmatch(info, " , ") do
if not plr1Done then
plr1 = i
plr1Done = true
else
plr2 = i
end
end
local plrWork = game.Workspace:FindFirstChild(plr1)
local plrWork2 = game.Workspace:FindFirstChild(plr2)
plrWork.Position = plrWork2.Position + Vector3.new(0,2,0)
end
function module.changeTime()
local done = false
local hour
for i in string.gmatch(game.Lighting.TimeOfDay, ":") do
if not done then
hour = tonumber(i)
done = true
end
end
if hour > 7 and hour < 13 then --checks if time is between day and midday
game.Lighting.TimeOfDay = "19:00:00"
else
game.Lighting.TimeOfDay = "07:00:00"
end
end
function module.checkCommand(commandText,aditionalInfo)
print("comm: ",commandText," moreinf: ",aditionalInfo)
if aditionalInfo == nil or aditionalInfo == "" then
local func = commands[commandText]
if func then
return func
end
else
local func = commands[commandText]
print("lehFun2", func)
if func then
return func
end
end
end
return module
I feel like i'm just not calling it properly, since this is the first time i'm learning about and using module scripts. If you have any tips and/or useful insights, feel free to share. :)
