Help with Synchronizing the Client and Server

But if I tween on the server, will it be a big deal? Like is it so bad? I mean I know it lags.

Not really, sure there will be some lag because Roblox servers are unoptimized for that thing but if it’s easy on you just do it on the server

Honestly at this point I must admit that the system I suggested needs to be polished more so I’m sorry. Tweening in the server should be ok but be careful about how many tweens you have running at the same time though

1 Like

It’s impossible. There is always a delay between the server and the client and vice versa. The client needs to update anytime the server updates through replication.

I recommend you checking this out.

1 Like
-- Server Script
local RS = game:GetService("ReplicatedStorage")
local rE = RS:WaitForChild("RemoteEvent")

local TS = game:GetService("TweenService")

local function tweenOnServer(object, charHrp)
    local tweeningInfo = TweenInfo.new(1.5)
    local goal = {CFrame = charHrp.CFrame}

    TS:Create(object, tweeningInfo, goal):Play()
end

while true do
    task.wait(5)

    for _, player in pairs(game:GetService("Players"):GetPlayers()) do
        local char = player.Character
        if char then
            local hrp = char:FindFirstChild("HumanoidRootPart")

            local clonedSphere = sphere:Clone()
            clonedSphere.Parent = workspace

            clonedSphere.CFrame = workspace.cylinder.CFrame

            task.wait(0.1)

            rE:FireClient(player, clonedSphere, hrp)
            tweenOnServer(clonedSphere, hrp)

            task.wait(1)

            clonedSphere:Destroy()
        end
    end
end

Local script:

local RS = game:GetService("ReplicatedStorage")
local rE = RS:WaitForChild("RemoteEvent")

local TS = game:GetService("TweenService")

local function tweenOnClient(object, charHrp)
    local tweeningInfo = TweenInfo.new(1.5)
    local goal = {CFrame = charHrp.CFrame}

    TS:Create(object, tweeningInfo, goal):Play()
end

rE.OnClientEvent:Connect(function(object, charHrp)
    tweenOnClient(object, charHrp)
end)
  1. I added the missing sphere variable declaration in the server script.
  2. The server script now fires the RemoteEvent for each player individually using rE:FireClient(player, clonedSphere, hrp) to avoid unnecessary data sent to all clients.
  3. The server script calls the tweenOnServer function to tween the sphere’s position on the server side.
  4. The client script calls the tweenOnClient function to tween the sphere’s position on the client side.
1 Like

For #2 that’s already what FireAllClients does with the benefit of not needing to loop for every player in the game

Edit: Also if you’re going to tween on the client anyway then there’s no need for the server tween

1 Like

The ball will just look glitchy because it’s being tweened at the server and at the client at the same time, in which the two has different runtime because of replication and ping, which I mentioned above.

1 Like

I’ve made a blade ball “ball” myself before and I made it client sided only but it was 2 laggy so what I did was I replicated it from the server to client to all players.

1 Like

I re-read the scripts on the main post very carefully and maybe this is what you actually want:
Server:

local players = game:GetService"Players"
local debris = game:GetService"Debris"

local remoteEvent = game:GetService"ReplicatedStorage".RemoteEvent

local sphere = game:GetService"ServerStorage".Part

local cylinderCFrame = workspace.Cylinder.CFrame

while true do
	task.wait(5)

	for _, player in players:GetPlayers() do
		local hrp = (player.Character or player.CharacterAdded:Wait()).PrimaryPart

		local sphereClone = sphere:Clone()
		sphereClone.CFrame = cylinderCFrame
		sphereClone.Parent = workspace

		remoteEvent:FireAllClients(sphereClone, hrp.CFrame)

		debris:AddItem(sphereClone, 2) -- Needs to be more than tween duration
	end
end

Client:

local tweenService = game:GetService"TweenService"

local tweenInfo = TweenInfo.new(1.5)

game:GetService"ReplicatedStorage".RemoteEvent.OnClientEvent:Connect(function(sphereClone, hrpCFrame)
	if not sphereClone then return end -- Just incase sphereClone is nil

	tweenService:Create(sphereClone, tweenInfo, {CFrame = hrpCFrame}):Play()
end)
1 Like

Wait, what makes Debris Service better than the :Destroy() Method and the script you edited is still a problem to hackers right? Since you didn’t move the position of the sphere on the server.

1 Like

Debris Service is just same as Destroy but with added timer (default is 5-8 seconds i think) but internally i assume it still calls Destroy() but also checks if object exists aswell so it doesn’t error if the object was destroyed prior Destroy() being called again

2 Likes

debris makes sure the object still exists after the duration, before it’s destroyed else you’ll get an error

I based these new scripts off of the ones on the main post, just optimized them a bit and made them tidier

2 Likes

I just tested it out and it turns out you’re right. I take back what I said. Also the Debris method is better than Destroy to be safe.

2 Likes

It is impossible to synchronize the client and server because both can be delayed cause of multiple things.

For exemple, the client can have delay because of its FPS, and the server can have delay because ressources taken, network ect… so even if you can minimize this delay for people that have a good PC and a good network, it will not be the same for everyone.

The only thing you can do in your case is to use a remote event, and Fire it from server to all client at each start and end of attacks.

1 Like

Is it worth it to tween in both the client and server though? Like, what if tween it on the server only?

1 Like

@T3_MasterGamer explained to me in a message that if you want to detect if the sphere hit something using Touched event on the server (which I also recommend for security purposes) and the sphere is being created on the server, you will need to move it on the server as well else the sphere will only move on the client causing the event to work incorrectly

1 Like

If you tween it on the server only, it’s more likely that it will look laggy on the client.

1 Like

Although if the client is experiencing network (internet) issues there will be a mismatch between the server tween and the client tween that I confirmed with a test just now (the server tween will complete before the client tween does even with the exact same tweenInfo configuration)

There isn’t a way that I know that can solve this problem unfortunately

1 Like

So, should I tween on both sides?

1 Like

Think about the problem: Us programmers and game designers rely on the server to secure and/or relay (replicate) data being sent to players in the game. When you play the game using the Roblox Player or website (since Studio uses your PC as a server for testing) Roblox selects a random server that’s available to host your game (from what I know it will try to find a server close to your locality when possible to minimize any delays). The server no matter where it’s located also relies on the client’s internet connection to do its job, so if the client has a bad internet connection they might either miss data being sent altogether or retrieve the data too late. The only way to solve this problem is by forcing players to have a good internet connection but obviously this isn’t possible and also has a risk to loose out on a lot of possible players since unfortunately good internet connections aren’t cheap (I speak from personal experience, my internet connection enjoys randomly disconnecting when it feels like it)

1 Like