How would I be able to open a door on the client from the server using tweens?

On the server I want to fire the client every time a player touches the hitbox for my door. The problem I have is that my tweens use tween.Completed:Wait() and that doesn’t wait on the server, so I end up firing the client very quickly and it messes up my tween.

This is my server side script if this helps

function module.onInstanceAdded(v)
	local leftDoorPos = v.LeftDoor.PrimaryPart.CFrame
	local RightDoorPos = v.RightDoor.PrimaryPart.CFrame
	
	v.PrimaryPart.Touched:Connect(function(part)
		local player = Players:GetPlayerFromCharacter(part.Parent)
		if player and player.Character then
			if isOpen then return end
			isOpen = true
			if not table.find(hitTable,player.UserId) then
				table.insert(hitTable,player.UserId)
			end
			Remotes.SlidingDoorAnimation:FireClient(player, hitTable, leftDoorPos, RightDoorPos, v)
		end
	end)
	
	v.PrimaryPart.TouchEnded:Connect(function(part)
		local player = Players:GetPlayerFromCharacter(part.Parent)
		if player and player.Character then
			isOpen = false
			local index = table.find(hitTable,player.UserId)
			if index then 
				table.remove(hitTable,index)
			end
			print(hitTable)
			Remotes.SlidingDoorAnimationClose:FireClient(player,hitTable)
		end
	end)
end

This is an example of what I am trying to achieve

2 Likes

Use a remote event to open or close the door and only make the tween on the client, a player’s character is client sided so they will be able to pass the door if it’s opened on the client and not the server

4 Likes

As @Aqua_Turneur has already said, you need to use a Remote event to transfer data from the server to the client and vice versa.

Read more here:

1 Like

Though using remote events is a viable solution, I would also consider the purpose of your doors, because I would argue that processing these things on the server wouldn’t be necessary if they are just doors that open for players!

Obviously, this is truly dependent on your game! If this is an event that must must MUST be replicated on the server and client, then by all means a remote event is what you need. Otherwise, you could really just set up these connections and render animations on the client only! Less unnecessary network throttling and more immediate responses.

If you’re going for the “replication” process, as Aqua has said, use a remote event to fire to all clients to fire the animation, and on the server, wait an estimated time for the tween to complete before updating the door’s CFrame! Replicating states like this can be pretty complicated, so having a client-sided door would be a great option :slight_smile:

4 Likes

So I’m going to be honest here, I put a local script in the workplace and tried the touched event and when it didn’t fire I assumed that touched events don’t work on the client. I didn’t consider that I needed to put the local script in starterplayer scripts. I don’t care if the server sees the door open, all I care about is the specific client being able to walk through the door, so this would have saved a lot of time.

Thank you, ended up using a remote event to fire to the client to start the tween, and another remote event to fire back to the server to let the server know whenever the tween was finished.

Ahh, then remote events are not necessary at all in this case, no need to send any data between server and client at all! If you have a controller (local script) that handles the touched connections and door tweens on the client (and placed in a local container like starter player scripts), you shouldn’t have any trouble rendering these effects with little to no delay! Let me know if you need help with that :slight_smile:

1 Like

Honestly how much of a performance gain would you be getting from having it done on the client rather than the server?
Are Tweens that harsh on performance seeing as you are just rotating an anchored hinge Part?
All the tweened doors I have are run on the server and never have an issue.

1 Like

Not really a performance problem, tweens just run smoother on client.

1 Like

Ah, mine don’t seem to have any smoothness issues.

1 Like

Tweens (and really any visual effects) are much smoother when ran on the client. My guess as to why they are choppier when processed on the server is because it needs to replicate the effect to all the clients. It totally depends on the game you’re making, but running a significant tweens on the server would not only be unsmooth, but would also cause huge performance issues! So rule of thumb is just to run these types of tweens on the client (unless you REALLY need to run it on the server, but I can’t think of a reason for that :thinking: ) :smiley:

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.