Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Kick script not working?

Asked by 7 years ago

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

Log in to vote
2
Answered by
M39a9am3R 3210 Moderation Voter Community Moderator
7 years ago
Edited 7 years ago

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.
0
Oops, my answer was wrong wasn't it xD User#11440 120 — 7y
0
Does this need to be a local script or a normal script? Whenever I do a normal script it errors. DeveloperSolo 370 — 7y
0
Needs to be a server script, what's the error? M39a9am3R 3210 — 7y
0
No errors. It is a server script in ServerScriptStorage DeveloperSolo 370 — 7y
View all comments (4 more)
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? M39a9am3R 3210 — 7y
0
Actual testing in the server. I use f9 for console DeveloperSolo 370 — 7y
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. M39a9am3R 3210 — 7y
0
Ah it works now DeveloperSolo 370 — 7y
Ad

Answer this question