Module Script not finding my user

I’m trying out module scripts with commands. The first script checks if the player is actually in the game. Once it verifies that the player is valid, it fires the function in the module script. I’m unsure why the second check in the module returns that it couldn’t find my user even though its in the game.

Command runner script (The one that says the player is in game)

local BasicMod = require(script.Parent.MainModule)

game.Players.PlayerAdded:Connect(function(plr)
	plr.Chatted:Connect(function(msg)
		local splitMsg = string.split(msg, " ")
		local cmd = splitMsg[1]
		if cmd == "test" then
			local target = game.Players:FindFirstChild(splitMsg[2])
			if target then
				print("Found player! (script)")
				BasicMod:Kick(target)
			end
		end
	end)
end)

Module script (Can’t find the player)

local BasicMod = {}

function BasicMod:Kick(player, reason)
	local target = game.Players:FindFirstChild(player)
	if target then
		print("found! (module script)")
	else
		print("not found! (module script)")
	end
end

return BasicMod

Output

image

This is the chat message in case it helps.
image

Bumping because of no replies yet.

Honestly, when I tested it, it seemed to work fine. In Studio, sometimes you load too fast for the server itself, which can cause PlayerAdded to not be recognized properly, perhaps that could be the issue. To address this, I created an onPlayerChatted function for you and added a simple loop that checks the players currently in the game:

for _, plr in pairs(game.Players:GetPlayers()) do
	plr.Chatted:Connect(onPlayerChatted)
end

Here’s your updated module, as I noticed you were trying to look for the target player twice:

local BasicMod = {}

function BasicMod:Kick(target, reason)
	if target then
		print("found! (module script)")
	else
		print("not found! (module script)")
	end
end

return BasicMod

And the updated main script:

local BasicMod = require(script.Parent.MainModule)

function onPlayerChatted(message)
	local splitMsg = string.split(message, " ")
	local cmd, targetName = splitMsg[1], splitMsg[2]
	if cmd == "test" then
		local target = game.Players:FindFirstChild(targetName)
		if target then
            BasicMod:Kick(target)
			print(debug.traceback(`Found user {target.Name} :-)`))
		end
	end
end

for _, plr in pairs(game.Players:GetPlayers()) do
	plr.Chatted:Connect(onPlayerChatted)
end

game.Players.PlayerAdded:Connect(function(plr)
	plr.Chatted:Connect(onPlayerChatted)
end)

Feel free to reach out if you have any questions or encounter any further issues.
image

It works! Thanks a lot.

Characters.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.