Client replicating door script

I have created a door which detects for player input on the server and then fires all clients to then initiate the door tweening. By doing this it will make the door move smoothly on any laggy clients opening the door.

I have came across a problem. The problem is that sometimes after a player requests to open the door, the door rotation will make the door close and open at incorrect angles. I think it has to do with client lag from the client, when the server is telling the client to tween the door, when the door hasn’t shut yet on the client.

Server script:

    for i,v in pairs(Door:GetChildren())do
    		if v:FindFirstChild("ProximityPrompt") then
    			v.ProximityPrompt.Triggered:Connect(function(Player)
    				if Toggle == false then
    					Toggle = true
    					if v.ProximityPrompt.Value.Value == "Left" then
    						Rotation = -100
    					else
    						Rotation = 100
    					end
    			
    game.ReplicatedStorage.Remotes.DoorEvent:FireAllClients(DoorPrimary, Rotation)
    					wait(6)
    					Toggle = false
    				end
    			end)
    		end
    	end
    end

Client script:

local TweenService = game:GetService("TweenService")

game.ReplicatedStorage.Remotes.DoorEvent.OnClientEvent:Connect(function(DoorPrimary, Rotation)

		TweenService:Create(DoorPrimary, TweenInfo.new(), {CFrame = DoorPrimary.CFrame * CFrame.Angles(0,math.rad(Rotation),0)}):Play()
		wait(5)
		TweenService:Create(DoorPrimary, TweenInfo.new(), {CFrame = DoorPrimary.CFrame * CFrame.Angles(0,math.rad(-Rotation),0)}):Play()
end)

Another question I had is about the possible exploits of the door, the tween is done on the client which means they could exploit it, but I’m not sure this would matter anyway because the client can easily abuse the Can-Collide property to walk through the door anyway. Any help or suggestions would be appreciated.

2 Likes

Belongs in #help-and-feedback:scripting-support.

1 Like

My bad, I am extremely new to devforum.

Don’t worry about this, if they made the door into a pancake it wouldn’t matter for anybody else since it’d be the same for them.

I’d reccomend creating a sort of ‘Tweening Module’ in the server, and a ‘Client Tweening’ starter player script.
Client Tweening handles the smooth animations etc, as normal, but as it is local, you need A. Remote Events for each tween type [stick them in replicated storage in a folder or something] and B. Server replication.
To achieve that, the Tweening Module can be used. If a script calls
Module.TweenPart(Part, ToGo, Time)
or something like it, then the function handles the rest. It could look something like:


--[[ Server tweening module ]]--
local module = {}
local TweeningFolder = game:GetService("ReplicatedStorage"):WaitForChild("Tweening")

module.TweenPart = function(Part, ToGo, Time)
    TweeningFolder.TweenPart:FireAllClients(Part, ToGo, Time) -- play animation
    wait(Time)
    Part.CFrame = ToGo -- update for the server
end

return module

then on the CLIENT in a LocalScript:


--[[ Client tween player ]]--
local Tweening = game:GetService("TweenService")
local TweeningFolder = game:GetService("ReplicatedStorage"):WaitForChild("Tweening")

local TweenPart = function(Part, ToGo, Time)
    local info = TweenInfo.new(Time, Enum.EasingStyle.Sine, Enum.EasingDirection.InOut, 0, false, 0)
    Tweening:Create(Part, info, {CFrame = ToGo}):Play()
end

TweeningFolder.OnClientEvent:Connect(function(Part, ToGo, Time)
    if not part then return end --ensure streaming enabled doesn't break this
    TweenPart(Part, ToGo, Time)
end)

And there you go, it updates instantly for the server with no animation for no lag, and it’s streaming enabled friendly!
This makes sure that the animation is smooth and it ensures that the part updates regardless of whether it plays or not.

To get this inside of any script define it inside of a require() function.

3 Likes

To make your door work with this, you could try:

  • Make positions for it, parts rotated or moved to where the door will end up.
  • Require that module and use it to move the door to another part’s CFrame to ensure that it goes to the exact spot you want it to.
1 Like

I have implemented this module and it works very well, thanks. Is there anyway I could use the module to tween the orientation of the doors hinge instead of the CFrame? So instead of adding on -90 degrees to the rotation every time I open the door, It would instead just set a target rotation vector, like (0,90,0).

Best way to do that is to weld the door to the hinge part / or set a primary part where a hinge should be and weld it to that.
Then tween the hinge part and the door should move with it, same as how you’d go about tweening a model.

1 Like