Making a game multiplayer-compatible

When you join this game, you’re able to control a part, instead of your character. The problem is, I can only get it to work with 1 player. If I test it with multiple players, then it glitches, and only allows one player to control the sphere. Does anyone know how to allow multiple players to control individual spheres?

You’d want to set up a system where the players each get given a copy of a default character sphere, and let them control it on the client.

Send what they do to the server, and then to all of the other clients. Do this for everyone, and you’ll have your own replication system.

It’s insecure against exploiting, but it’s simple and there are ways around that that are best left to after you’ve made your system.

I’m not exactly sure how to do this… would you mind explaining it in more detail?

Well the complexity depends on whether you are in experimental mode - if you are, all you need to do is give em all a ball and let them control.

If you’re not, I’ll go into it after the first stage.

Stage 1: Giving players a character

When a player joins, give them a character and parent it workspace.

local Players = game:GetService("Players")
local CharacterTemplate = game:GetService("ServerStorage"):WaitForChild("Character")

Players.PlayerAdded:Connect(function(Player)
    local Character = CharacterTemplate:Clone()
    Character.Name = Player.Name
    Player.Character = Character
    -- make sure to set Player.Character BEFORE you parent it to workspace so CharacterAdded fires
    Character.Parent = workspace
end)

On the client, to control the character, you’d want to let them control their character. You could use UserInputService to make things happen when the player presses a key or button.

-- put this in StarterPlayerScripts so it doesn't reset when the character respawns
local Player = game.Players.LocalPlayer

Player.CharacterAdded:Connect(function(Character)
    CacheCharacter = Character
end)

UserInputService.InputBegan:Connect(function(Input,GP)
    if CacheCharacter and not GP and Input.KeyCode == Enum.KeyCode.W then
        -- move the character forward
        CacheCharacter.Position = CacheCharacter.Position + Vector3.new(1,0,0)
    end
end)

This will automatically replicate if experimental mode is on.

Stage 2: Custom replication

RemoteEvents! Send one to the server, then from there to other clients.

-- in the client
-- to send
Replication:FireServer(Character.Position)

--- to recieve
Replication.OnClientEvent:Connect(function(Player,Position)
    game.Workspace:FindFirstChild(Player.Name).Position = Position
end)

And on the server, you want to do all the distribution and validation:

Replication.OnServerEvent:Connect(function(Sender,Position)
    for _,Player in pairs (game.Players:GetPlayers()) do
        if Player ~= Sender then
            Replication:FireClient(Player,Sender,Position)
        end
    end
end
1 Like

Thanks for taking the time to write that script. I’m going to try it now, just to make sure everything in the game works efficiently.