Best way to code a door that is getting tweened after ClickDetector

So I want to make a door that closes like FNAF as ideal as possible.
So the player presses the button, then the door closes, Then a bool value is assigned true/False depending on what it is now.
The main problem that I have is that if I handle all that on server, it will lag. If I put it on client, the value can be changed by the player through programs.
So if that’s it, then how should I do it? Use remote events, so the server handles the value and the client the tweened part? If I do that, how will the script check when the tween ended on client so the button can be pressed again, on server since there should be a deounce?

Hi!

Hmm, there is often a tradeoff between security (doing things on the server) and responsiveness (doing things locally). But in this case I am not sure what exactly you are aiming for. You want to prevent the player from cheating, but if they cheat they can on their own client open the door or just teleport across it all they want anyway.

Probably, if a player clicks to open a door, it should open on all clients (so for all players). That means it has to go through the server. But, as you say, tweening on the server makes everything really laggy.

So normally, I would say the best solution is as follows:
The player presses the button, it sends a message to the server through remoteEvent.
The server checks if the player is actually close to the button, and if all security checks work out, it sends a message to all players in the game to open the door (or close it) by remoteEvent.
The players all listen to this event and open or close the door accordingly by local tween. If your game is big, you may want to ignore far away doors.

But since each client keeps track of which doors are open or closed, by remembering the last remote event, new players joining the game (or players coming into range, if you ignored far away doors) will need to be initialized. That means when they are ready and listening to the door’s remote event, they send a message request for initialization, and the server responds with a message of the current door status (open or closed) to only that client. You can save some code by just sending the normal ‘open’ or ‘close’ message to just that player.

Both the client and the server can ignore new button clicks before some cooldown, equal to the tween time. Probably it’s no problem if initialisation also gives the button click cooldown on the initializing client, or else you can sent an extra parameter in that case to ignore the cooldown: tweens can override each other anyway, so it should be fine. Note that the server makes sure incoming clicks (through remote event) before the cooldown is over, are ignored, to handle multiple people clicking the button at the same time and to prevent cheaters.

Hope this helps, this is probably how I would do it. Good luck!

2 Likes

Good solution, but the players joining wouldn’t be a problem since it’s a singleplayer game. Should’ve pointed that one out first. Anyway thx!

1 Like