i am wondering how to do it it’s for a game
- Create a table of players who joined the server
- When player joins, check if they are in that table
- If they are kick them; if not, add them to the table
This is just a basic idea:
local joined = {}
game.Players.PlayerAdded:Connect(function(Plr)
task.wait(1)
if table.find(joined, Plr) == nil then
table.insert(joined, Plr)
else
Plr:Kick("You have already joined this server. Please join a new one.")
end
end)
You would probably want to save a table of JobIds
that the player has been in inside a DataStore
and check all of them against the server the player is joining.
local PlayersJoined = {}
game:GetService("Players").PlayerAdded:Connect(function(player)
if table.find(PlayersJoined, player) == nil then
table.insert(PlayersJoined, player)
else
player:Kick("you already joined this server")
end
end)
i don’t think i would need a data store cause once everybody leaves the server, the server deletes itself
Thats not what I meant. I mean every player has a list of servers they have joined saved, not every server have a list of players that have joined.
that didn’t work i don’t know what could be wrong
Is there a error or something ?
nope, there are no errors i don’t know what it is
How are you testing it? On studio? Because you would need to test it on an actual game with at least 2 players on the server (since the server gets destroyed after last player leaves)
yeah im testing it in a real server with my alt account
If you’re having an issue with the PlayerAdded function not firing when you first join into the server you could try this:
local joined = {}
-------
local functions = {
["Plr_ADDED"] = function(Plr)
task.wait(1)
if table.find(joined, Plr) == nil then
table.insert(joined, Plr)
else
Plr:Kick("You have already joined this server. Please join a new one.")
end
end,
--add more here:
}
game.Players.PlayerAdded:Connect(function(Plr)
functions["Plr_ADDED"](Plr)
end)
------
task.wait(1)
if #game.Players:GetChildren() <= 0 then
repeat
wait()
until #game.Players:GetChildren() > 0
end
for i,Plr in pairs(game.Players:GetChildren()) do
functions["Plr_ADDED"](Plr)
end
Oh sorry I meant to put .Name/.userId not the player instance
local PlayersJoined = {}
game:GetService("Players").PlayerAdded:Connect(function(player)
--player:Kick("exited")
if table.find(PlayersJoined, player) == nil then
table.insert(PlayersJoined, player.UserId)
else
player:Kick("you already joined this server")
end
end)
This is better, I thought I put .UserId earlier
ok ill try it!!!
local Joined = {}
game.Players.PlayerAdded:Connect(function(Player)
if Joined[Player.UserId] then Player:Kick("You have already joined this server") return end
Joined[Player.UserId] = true
end)
still didn’t work no errors i put my name into test if it works and it didn’t kick me
local PlayersJoined = {
"CookieTheSoda"
}
print(PlayersJoined)
game:GetService("Players").PlayerAdded:Connect(function(player)
--player:Kick("exited")
if table.find(PlayersJoined, player) == nil then
table.insert(PlayersJoined, player.UserId)
else
player:Kick("you already joined this server")
end
end)
had to fix up the script a litte for anybody wondering what the script is it’s
local PlayersJoined = {}
print(PlayersJoined)
game:GetService("Players").PlayerAdded:Connect(function(player)
--player:Kick("exited")
print(player.Name)
if table.find(PlayersJoined, player.Name) == nil then
print("player not found")
table.insert(PlayersJoined, player.Name)
else
player:Kick("you already joined this server")
end
end)
Haha sorry I am a little tired, I peeped I only put .UserId on only one place instead of two. But glad you got it to work!