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

Kick script not working?

This is a local script inside of starter gui

AlphaTesters = {"Player1", "BeafBomber",  "Sxult"}

game.Players.PlayerAdded:connect(function(newPlayer)
	for i,v in pairs(AlphaTesters) do
		if v.Name == newPlayer then
			print('Person with access joined')
		else
			v:Kick("Sorry, this game is in closed Alpha".."\n".."Only Developers/Alpha testers may enter")
		end
	end
end)

Right now the game is in alpha and I only want testers/developers to join. I made this script to prevent people. Strangely when I first made it, it worked. Then at a random moment it broke. (Probably studio crashed and I didn't see the change qq)

P.S, I didn't add my name so I could test it myself.

Thanks.

1 answer

✓ Accepted answer

##Problem

What is happening is when the first player joins the game, they have to have the name Player1. Otherwise they will get kicked no matter what.

Because the for loop will go into an if then statement which will check, alright so the player's name is not Player1, kick him. Oh the player's name was "BeafBomber", oh well he was already kicked since we were asked.


##Solution

You can do what many admin scripts do. Call a function that will go through the for loop and return true if the name is correct, or return false once the for loop terminates.


##Final Script

AlphaTesters = {"Player1", "BeafBomber",  "Sxult"}

function IsAnAlphaTester(Player)
    for _,v in pairs(AlphaTesters) do
        if v:lower() == Player.Name:lower() then --:lower() will make the entire name or string lowercase, as to not be worried on case sensitivity.
            return true --At this point, we know the player's name is in the list, so we will tell whoever called the function the good news.
        end
    end
    return false --If the player's name was not found by now, it never will...
end

game.Players.PlayerAdded:connect(function(newPlayer)
    if IsAnAlphaTester(newPlayer) then
        print('He is testing the game.')
    else
        newPlayer:Kick('Sorry, game is in closed alpha testing. Try again later.')
    end
end)

##Credit Due

wfvj014 ended up mentioning this, you were getting your variables mixed up because you tried to kick a string value "v". Not the "newPlayer" who kick was supposed to be called on. Good catch wfvj014!

BStandsForBuilding mentioned this as well. Kick will not work as intended in a LocalScript. Same with the PlayerAdded event in StarterGui, you need to move the script to a Server Script and you will need to move the script to ServerScriptService or Workspace.


######Hopefully this answered your question. If it did, do not forget to hit the accept answer button. If you have any questions, feel free to comment below and I will get back to you.

8 comments

0

Oops, my answer was wrong wasn't it xD

0

Does this need to be a local script or a normal script? Whenever I do a normal script it errors.

0

Needs to be a server script, what's the error?

0

No errors. It is a server script in ServerScriptStorage

0

Well, what's the problem? How are you testing it? Are you in Studio Test mode or in an actual server testing this script?

0

Actual testing in the server. I use f9 for console

0

Did you check the Server Logs tab? Are you getting kicked? Try printing some lines to see if the Kick command processes or not. You could wait for the Character to load by adding newPlayer.CharacterAdded:wait() before the if then statement.

0

Ah it works now

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