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

ModuleScript not printing?

I'm working on some admin commands as a little side project and I've come across a problem. My chatted event fires, but my module code doesn't...

local Commands = {}

function Commands.KillPlayer(playertoKill)
	print("got here")
	if game:FindFirstChild(playertoKill) ~= nil then
		print("got here 2")
		workspace[findPlayer(playertoKill)]:BreakJoints()
	end
end

function findPlayer(playerName)
	 for _, player in ipairs(game.Players:GetPlayers()) do
        if player.Name:lower() == playerName:lower() then
            return player
        end
    end
end

return Commands

The main script in ServerScriptService

local Commands = require(game.ServerScriptService.Commands)
 
function onChatted(message, player)
    if message:sub(1,5) == "!kill" then
		Commands.KillPlayer(message:sub(7)) -- I've tested and it definitely gets to this point
	end
end
 
game.Players.PlayerAdded:connect(function(player)
	if player.Name == "MasterDaniel" then
    			player.Chatted:connect(function(message) 
			onChatted(message, player) 
		end)
	end
end)

Does anyone know why it is broken? Nothing in the module prints, even though KillPlayer is definitely called from the main script.

6 comments

0

That's not a connection, and that won't work. Please don't give this misinformation again.

0

Try using a method ':' to index/call your function(s). So instead of 'Commands.KillPlayer()' do 'Commands:KillPlayer()', and index respectively.

0

actually using . is fine, its the way he uses workspace[findPlayer)

0

The thing is, the module never prints "got here", and from previous tests I know that the if statement in the other code is correctly evaluated.

0

Alright, Im going to completely redo this. There is so many different ways you can go with this.

0

Mine works fine, what are you talking about...

1 answer

Okay.. Im on tablet, so.. I cant explain this the way I want to. But I created this code. It has a really good chance it will work, but if it doesn't. PM me asap.

You better accept this answer if it works, else I just spent over a hour of my time for nothing. :P

ModuleScript: (Named: ModuleScript; Parent: ReplicatedStorage)

local _FUNCTIONS={}
local Commands={}

function _FUNCTIONS:GetPlayer(Message)
	local Player_m = string.lower(Message) -- Player -> player
	local Name_s = #Player_m -- to a number
	for Null,Plr in next,game.Players:GetPlayers() do 
		if tostring(Plr):lower():sub(1, Name_s) == Player_m then -- Front of their name
			return(Plr) -- Returns a userdata, if player found
		end
	end
end

Commands={
	{Command={"Kill/"; "/K/"}; Function=function(Owner, Message)
		if (Message == nil or not Message) and type(Message) == "string" then
			return -- Stop the code
		end
		local Player = _FUNCTIONS:GetPlayer(Message)
		if Player~=nil then
			spawn(function()
				Player.Character.Humanoid.Health=0
			end)
		end
	end}
}

return(Commands)

Server script: (Named: Script; Parent: ServerScriptService)

local Players=game:GetService("Players")
local Commands=require(game:GetService("ReplicatedStorage"):WaitForChild("ModuleScript"))
local _CORE={}

local Admins={
	"Player";
	"Player1";
}

function _CORE:GetRank(Player_Name)
	for Null,Value in next,Admins do
		if Player_Name:lower() == Value:lower() then
			return(true)
		end
	end
	return(false)
end

function _CORE:ConnectUser(Player)
	local con; local con2
	con=Player.Chatted:connect(function(Message)
		if _CORE:GetRank(tostring(Player)) == true then
			if Message:lower():sub(1,3) == "/e " then
				Message=Message:sub(4)
			end
			for _,v in next,Commands do
				local Command_t=v.Command
				for _,v2 in next,Command_t do
					if Message:lower():sub(1, #v2) == Message:sub(1,#v2) then
						Message=Message:sub(#v2+1)
						v.Function(Player, Message)
					end
				end
			end
		end
	end)
	con2=Players.PlayerRemoving:connect(function(Player)
		con:disconnect()
		con2:disconnect()
	end)
end

if game:GetService("RunService"):IsStudio() == true then
	repeat wait(.5) until game.Loaded
	for _,p in next,Players:GetPlayers() do
		_CORE:ConnectUser(p)
	end
end

Players.PlayerAdded:connect(function(p)
	_CORE:ConnectUser(p)
end)

2 comments

0

It didn't work :/. I'm not sure whats going wrong with this.

0

Try kill/pla

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