Having some ModuleScript problems

So I’m currently having some ModuleScript problems. So I want the owner of the game (when I release my model), I want them to be able to choose if they want to lock their game to only the whitelisted. But for some reason the other module script isn’t kicking them. I will leave the script exerpts below:

--script exerpt 1
CCentralData.GameLocked = true
--script exerpt 2
if CCentralSettings.GameLocked == false then
		return nil
	else
		for i,whitelist in pairs(CCentralSettings.Whitelisted) do
			if plr.Name == whitelist then
				return nil
			else
				CCMainData:Kick(plr)
			end
		end
	end
--script exerpt 3
function CCMainData:Kick(plr)
	plr:Kick("You were kicked from this game because you are not whitelisted. This game is locked.")
end

Anyone have any tips or would like to point out something I missed?

3 Likes

It’s hard to know exactly what the issue is without a whole look at the modules/scripts you’re using.

Would adding the require()'s and the returns help?

1 Like

If you could post the entire scripts you’re using, I’d probably be able to help you more. Otherwise, it’s kind of hard to debug it.

On a side note, one thing I noticed:

for i,whitelist in pairs(CCentralSettings.Whitelisted) do
		if plr.Name == whitelist then
			return nil
		else
			CCMainData:Kick(plr)
		end
	end

You’re kicking the player without actually testing all the entries in the Whitelisted table. So if the first index isn’t the player, it’ll kick them, even though that player might be in the Whitelisted table at a later index. Using a dictionary format would be easier, and you wouldn’t even have to use a loop to use it.

Example:

local whitelist = {
["SamuelSmith"] = true;
["SomebodyElse"] = true;
}

local function onJoined(player)
if not whitelist[player.Name] then
--//kick the player, they're not in the whitelist
end
end

game.Players.PlayerAdded:Connect(onJoined)

It has to be something to do with how one Module Script is receiving information from another because I tried both of our methods using the external module script and it didnt work. I made both module scripts by the way.

Wait a minute, I may have missed something.

Yeah still, I think it’s something wrong with the actual module script.

How are you indexing CCentralSettings.Whitelisted? My guess here is that plr.Name never equals whitelist, thus returning nil every iteration of the loop.

Do a quick test and put a print in CCMainData:Kick(plr) to see if it even makes it there in the first place. Double check your Whitelisted indexing and follow what C_Sharper said earlier about your recursive check issue.

Also, try to be consistent with your naming as not to get confused later on. Why is it called CCentralData.GameLocked in one place and CCentralSettings.GameLocked in another? Try not to confuse your (future) self.

Your example would not work. Instead, use table.find.

local whitelist = {"SamuelSmith", "JohnJones"}

local function onJoined(player)
if not table.find(whitelist, player.Name) then
--//kick the player, they're not in the whitelist
end
end

game.Players.PlayerAdded:Connect(onJoined)

Oh shoot, I did that wrong. Thanks for pointing that out

1 Like

@AlreadyPro
I guess this wasnt really my problem. The whitelist part wasnt my problem, its more of why isnt my function running. I did a test and here are my results:

So I set one Module Scripts value to true and from another module script I checked to see if that value was true and if it was true it would print something to the console, I did it and nothing happened. Does it his explain it a bit more? This is the code below:

CCentralSettings.PrintValue = true
game.Players.PlayerAdded:connect(function()
	if CCentralSettings.PrintValue == true then
		print("Value = true")
	end
end)

Does this help?

Are you testing that in Studio? I am pretty sure that Play in studio doesn’t fire off the PlayerAdded event. I suggest trying to do just a print(CCentralSettings.PrintValue) by itself instead.

I still get nothing from the console.

Ok, my assumption here is probably that you are requiring your ModuleScript
local CCentralSettings = require(pathTo.CCentralSettings)

But, that require is getting the return from CCentralSettings which may or may not contain the data you need to access in the return itself. What does your ModuleScript return look like? Are you just doing
return CCentralSettings ?

Yes I am, sorry for the late response.