I’m making a matchmaking system for my game, squared. I’m not completely sure if this script will work though, so can anyone point out anything that I should fix or change?
JoinScript in TextButton
local player = game.Players.LocalPlayer
local Button = script.Parent
local Remotes = game:GetService("ReplicatedStorage"):WaitForChild("Remotes")
local ConnectToChannel = Remotes:WaitForChild("ConnectToChannel") -- When fired, sends a connection to the server to make the player join the queue
local DisconnectFromChannel = Remotes:WaitForChild("DisconnectFromChannel") -- When fired, sends a connection to the server to let the server know that the player wants to leave the queue
local clicked = 0
script.Parent.MouseButton1Down:Connect(function() -- Checks if MouseButton1 Is fired.
if clicked == 0 then -- They want to join
Button.BackgroundColor = Color3.new(0.623529, 1, 0.596078)
ConnectToChannel:FireServer(player, 1) -- Connects the player to the match
elseif clicked == 1 then -- They want to leave
Button.BackgroundColor = Color3.new(1, 0.458824, 0.458824)
DisconnectFromChannel:FireServer(player, 1) -- Disconnects the player from the match
end
end)
MatchmakingScript in SSS
--\\ Varibles //--
local channels = { -- The Channels
channel1 = {}
}
local DataStoreService = game:GetService("DataStoreService")
local dataStore = DataStoreService:GetDataStore("Data") -- This can be changed to whatever you want
local Remotes = game:GetService("ReplicatedStorage"):WaitForChild("Remotes")
local AddPlayerToChannel = Remotes:WaitForChild("AddPlayerToChannel")
--\\ Functions //--
AddPlayerToChannel.OnServerEvent:Connect(function(player, channel) -- Adds the player to the channel
if channel == 1 then
table.insert(channels.channel1, player)
end
end)
while task.wait() do -- Checks if channel values have changed and updates them
for i, _ in pairs(channels) do
if #_ == 1 then
for index, player in pairs(_) do
local teleportData = dataStore:GetAsync(player.UserId) -- Sends in the player into the game with their respective data stores
local telaportOptions = Instance.new("TeleportOptions")
telaportOptions:SetTeleportData(teleportData)
game:GetService("TeleportService"):Teleport(11827447438, player, telaportOptions)
table.remove(_, player)
end
end
end
end
-- local channel = game:GetService("MemoryStoreService"):GetQueue("channel") channel:AddAsync("e", 400, 0) print(channel:ReadAsync(4, false, -1))
13:33:47.961 DataStore request was added to queue. If request queue fills, further requests will be dropped. Try sending fewer requests.Key = 1900786800 - Studio
13:33:48.359 Data has been saved! - Server
13:33:48.359 Data has been saved - Server
Datastore
-- // Assigning variables //
local DataStoreService = game:GetService("DataStoreService")
local dataStore = DataStoreService:GetDataStore("Data") -- This can be changed to whatever you want
local function saveData(player) -- The functions that saves data
local tableToSave = {
player.leaderstats.Wins.Value; -- First value from the table
}
local success, err = pcall(function()
dataStore:SetAsync(player.UserId, tableToSave) -- Save the data with the player UserId, and the table we wanna save
end)
if success then -- If the data has been saved
print("Data has been saved!")
else -- Else if the save failed
print("Data hasn't been saved!")
warn(err)
end
end
game.Players.PlayerAdded:Connect(function(player) -- When a player joins the game
-- // Assigning player stats //
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local Wins = Instance.new("IntValue")
Wins.Name = "Wins"
Wins.Parent = leaderstats
local data -- We will define the data here so we can use it later, this data is the table we saved
local success, err = pcall(function()
data = dataStore:GetAsync(player.UserId) -- Get the data from the datastore
end)
if success and data then -- If there were no errors and player loaded the data
Wins.Value = data[1] -- Set the Wins to the first value of the table (data)
else -- The player didn't load in the data, and probably is a new player
print("The player has no data!") -- The default will be set to 0
Wins.Value = 0
end
end)
game.Players.PlayerRemoving:Connect(function(player) -- When a player leaves
local success, err = pcall(function()
saveData(player) -- Save the data
end)
if success then
print("Data has been saved")
else
print("Data has not been saved!")
end
end)
game:BindToClose(function() -- When the server shuts down
for _, player in pairs(game.Players:GetPlayers()) do -- Loop through all the players
local success, err = pcall(function()
saveData(player) -- Save the data
end)
if success then
print("Data has been saved")
else
print("Data has not been saved!")
end
end
end)
13:40:18.730 Player: StarJ3M Has Joined Channel 1. - Client - Join:14
13:40:18.746 channel1 is not a valid member of Player "Players.StarJ3M" - Server - Matchmaking:20
13:40:18.747 Stack Begin - Studio
13:40:18.747 Script 'ServerScriptService.Matchmaking', Line 20 - Studio - Matchmaking:20
13:40:18.747 Stack End - Studio
AddPlayerToChannel.OnServerEvent:Connect(function(player, channel) -- Adds the player to the channel
if channel == 1 then
print("Added "..player.Name.." To Channel 1.")
table.insert(channels.channel1, player)
print(#channel.channel1)
else
print(#channel.channel1)
warn("issue")
end
end)
table.remove takes 2 paramaters, a table and an index. In this example you are only giving the table and the player. You should use table.find to find the index like this.