I’ve made a code that enters a player that touches a part to a table. I’m very new to this sort of stuff and experimented using what I read about tables. I’m wondering if I’m doing this right, or if there’s a better way to do this.
Local script:
local rStorage = game:GetService("ReplicatedStorage")
local plrJoinEvent = rStorage:WaitForChild("playerJoinEvent")
local plr = game:GetService("Players").LocalPlayer
local joinPart = game.Workspace.joinPart
local inQueue = false
while true do
wait()
joinPart.Touched:Connect(function(hit) -- Call function when player touches part
if not inQueue and hit.Parent == plr.Character then -- If not already in a queue and if the player character touched the part
plrJoinEvent:FireServer() -- Fire remote event
inQueue = true -- Player is in queue
end
end)
end
Server script:
local rStorage = game:GetService("ReplicatedStorage")
local plrJoinEvent = rStorage:WaitForChild("playerJoinEvent")
local queue = {}
plrJoinEvent.OnServerEvent:Connect(function(player) -- When remote event is fired, add player who fired it to queue
if #queue < 4 then -- If there is still room in the queue, add player
table.insert(queue, player)
print(#queue)
end
if #queue == 4 then -- if queue is full, teleport players and empty queue
for place,plr in pairs(queue) do
if place and plr then
print("teleport "..plr.Name)
end
end
queue = {} -- Empty queue
end
end)
I appreciate all answers, thanks in advance.