In my game, each player owns a plot with a set of nodes. A train is automatically tweened between these nodes using CFrame and Tween Service.
I want tweens to be as smooth as possible for the client and I am perfectly happy for the trains on someones plot to be manipulated by their client. Therefore I decided to set NetworkOwnership to the player for the train part.
However, the issue with CFrame tweening is that the train needs to be anchored. It has a very precise set of nodes to follow and Body Movers wouldn’t really work. Yet as everyone knows - NetworkOwnership does not work with anchored parts.
If anyone has any ideas of a way to achieve this - i’d really appreciate them! Tweening the parts server side is out of the question as in some cases, users can manually control the train which requires an instant response. Or maybe there is a way for me to tween the part locally and somehow efficiently replicate the movement to the other clients?
My suggestion is to use anchored parts; Have a copy of each train on the client side. That way, the client will see it smoothly, and you can just fire the client were to tween the trains too.
Not necessarily if this is purely Server > Client motion
I didn’t really consider sending the node path to each client as I thought there may be a lag issue if every client is processing the movement of every train on every plot.
--Server
local re = xxx--Remote Event
--Make "train" the corresponding of the train
re:FireAllClients(train,moveTo)
--Client
local re = xxx--Remove Event
re.OnClientEvent:Connect(function(train,to)
--Tween train to new position
end)
Obviously, these is some basic sudo code, but it should work pretty well.
Note: Check the position on the SERVER. Say you reward them 100 coins or something for doing x miles, store the position on the server and send it to the client.
The money awarding system shouldn’t be too hard as I will just award the user money whenever the server tell all clients too move X train to Y station.
Send event back to all clients (or all clients nearby the object in question) telling them to move the object in the desired direction.
So in your case this translates to the players controlling the train on the client, the action is verified on the server and the actual movement is done on each player’s client. (The tweening)
This is how I did the laser guns for one of my projects too, since giving the projectile NetworkOwnership didn’t work in all cases. Not sure if this would be a good way to handle vehicle movement though, but maybe worth a try.
The problem with this is that there can be delays through remote events. So you might want to first move the object on the client, then have the server verify it.
That’d work, the only issue with that is that players may see the player moving in “illegal” directions if they’re exploiting or lagging before the server verifies the action. (As player movement is replicated from the client to other clients) And unless the player’s ping is high it’s almost unnoticable if you keep it clean.
I tried a combination of body position and body gyro already but they seem to always require some sort of dampening and cannot pro odd instant movement.
They also act weird when manipulated multiple times every second.
After combining my own knowledge, some ideas here and private talks with other developers I have come to a final solution I think is right.
To move a train, the server fires a remote event for all clients (apart from the player) within X studs of the players plot so the train moves on those player’s screens.
The server then fires a remote function to the player that owns the plot which tells the train to move between Node A and B. The server waits for this function to finish (for the train to move) before continuing. This solution also stops any issues if the client freeze glitch occurs where a player can hold the X button to freeze all local scripts. If this happens, all other clients will just see the players train waiting at the next station/node
When a train arrives at a station. Another remove event is fired to all clients to make sure the train’s stable position is correct for everyone (e.g. new players)
That’s what i’ll be testing a LOT after implementing this. Hopefully the fact it only sends data to players within X studs should reduce the load. I will also make train movements as large as possible so the server isn’t constantly sending little increments to the clients.