Hello, I have created a nifty screen transition that would make the times teleportation is used less jarring for the user, but I’m not sure how to smoothly implement it to respond to the server. As of now, with the example below, it will start the transition after the teleportation has already occurred.
Here is an example of the problem:
SERVER
local transitionEvent = ReplicatedStorage.event
function someFunction(player: Player)
transitionEvent:FireClient(player)
teleportFunction() --obviously will run before the effect has completed
end
CLIENT
local transitionEvent = ReplicatedStorage.event
local fxModule = require(ReplicatedStorage.fxModule)
transitionEvent.OnClientEvent(function()
fxModule.ScreenTransition()
end)
I have tried using the :Wait() functionality in-between the event call and the teleport function on the server, but unless there is some way to filter what player invokes it - it breaks with > 1 client. I have also dabbled with the caveman task.wait() “solution”, but that only covers up the fundamental desync problem.
There should be a way to do anything like this. Could you explain this a little more? (E.g. your code?)
Like is the problem that your wait is causing someFunction to pause/yield? You can just wrap it in a task.spawn(function() [YOUR CODE] end).
One way to have this sync is to send the client a time that the teleport will start.
For example, do transitionEvent:FireClient(player, futureTime), then on the client plan on the teleport happening at that future time. (E.g. if the future time the client got is in 3 seconds and you have a 1 second animation, start the animation in 2 seconds).
What, we don’t get to see this nifty screen transition..
Well this sounds like a call back thing.. have a callback from the client back to the server after the screen transition finishes.
Server tells the client: start transition.
Client runs transition, then signals the server: done.
Server then only teleports after receiving that signal from that specific player.
Untested mock up of the steps:
Server
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local transitionEvent = ReplicatedStorage:WaitForChild("transitionEvent")
local transitionDone = ReplicatedStorage:WaitForChild("transitionDone")
function someFunction(player)
transitionEvent:FireClient(player)
transitionDone.OnServerEvent:Wait() --waiting for the client signals
teleportFunction(player)
end
Client
ocal ReplicatedStorage = game:GetService("ReplicatedStorage")
local transitionEvent = ReplicatedStorage:WaitForChild("transitionEvent")
local transitionDone = ReplicatedStorage:WaitForChild("transitionDone")
local fxModule = require(ReplicatedStorage.fxModule)
transitionEvent.OnClientEvent:Connect(function()
fxModule.ScreenTransition()
transitionDone:FireServer()
end)
This, but instead of having extra RemoteEvents, have the transition event be a RemoteFunction where the OnClientInvoke function will yield until the transition is done
I’m sure you can find many ways to pull this off.. it’s just a call back or a way to set that order of events. Overall I think this would be the best and easiest way to do that. Almost wrights itself.
Essentially the event:Wait() would stop yielding when any player invoked the server, which isn’t exactly ideal, and thus I was wondering if you could filter the player that for the :Wait(). I could post a less cookie cutter version of the code if that didn’t clear anything up.
Your possible solution does seem promising though, I’ll be sure to look into it.
Unfortunately the nifty transition isn’t that amazing, just imagine this thread’s solution Sqaure flood effect help, but more lazily implemented since I didn’t want to think about math more than I had to.
As for your solution, I have tested it, but it only ever yields/waits once.
As @fl4mxr1 stated, use a RemoteFunction to communicate with the client, which will yield for a callback to ensure the transition effect has ended before beginning teleportation.
SERVER
local transitionFunction = ReplicatedStorage.RemoteFunction
function someFunction(player: Player)
transitionFunction:InvokeClient(player) --this invoke yields for the client's return
teleportFunction() --will not run until client returns a value (or nil in this case)
end
CLIENT
local transitionFunction = ReplicatedStorage.RemoteFunction
local fxModule = require(ReplicatedStorage.fxModule)
function someFunction()
fxModule.ScreenTransition()
end
transitionFunction.OnClientInvoke = someFunction
local specificPlayer = ... -- The player you're waiting for
while specificPlayer ~= event:Wait() do print("not the player") end
-- (Make sure the event you're using is one that has a player as the parameter)
because event:Wait() returns the parameters that would normally go into the connected function.
(If you wanted it to be super perfect you would do some more stuff to include players leaving (because this could cause it to wait forever), but it’s just a rare small memory leak.)
I probably should’ve mentioned this in the original post, but someFunction() is handling more than one player. Since remote functions yield for each invocation it wouldn’t work as intended. The more lore accurate server code would be more like this:
local transitionEvent = ReplicatedStorage.event
function someFunction(players: {Player})
for _, player in players do
transitionEvent:FireClient(player)
end
teleportFunction(players)
end
local transitionFunction = ReplicatedStorage.RemoteFunction
function someFunction(players: {Player})
local invokeFinishedEvent = Instance.new("BindableEvent")
local successfulInvokes = 0
for _, player in players do
task.spawn(function() --call all invokes in parallel using threads to prevent yielding
transitionFunction:InvokeClient(player)
successfulInvokes += 1
if successfulInvokes == #players then
invokeFinishedEvent:Fire() --all players have finished their transitions
end
end)
end
invokeFinishedEvent.Event:Wait()
invokeFinishedEvent:Destroy() --cleanup
teleportFunction(players)
end
I tested this method pretty extensively, and unfortunately event:Wait() and while loops just don’t seem play nicely together. Specifically, if while specificPlayer ~= event:Wait() do is false on the first call, the thread will be caught in an infinite loop since there’s no way for the specificPlayer conditional to be updated. I did find what I think is solution using a similar train of thought that I will post soonish, so thanks for your input.
Comes extremely close to working for me, and I think this approach would work perfectly well for most of anyone that comes across this thread in the future, so I will mark it as the solution, and thank you for your help.
But it would unfortunately require me to rewrite a substantial amount of the visual effect code and other reasons that I doubt anyone cares to know, so I came up with something that seems to fulfill all my requirements using remote events:
SERVER
local function playerWaitRecursion(event: RBXScriptSignal, targetPlayer: Player, retries: number)
if retries <= 0 then return end
local playerResponse = event.OnServerEvent:Wait()
if playerResponse == targetPlayer then return end
local newRetry = retries - 1
playerWaitRecursion(event, targetPlayer, newRetry)
end
local transitionEvent = ReplicatedStorage.event
function someFunction(players: {Player})
for _, player in players do
transitionEvent:FireClient(player)
end
playerWaitRecursion(transitionEvent, players[1], 0)
teleportFunction(players)
end
The client side is unchanged from the original post, and playerWaitRecursion function should work in any real project that you’d put it into - although it could probably use some more rigorous logic to prevent the thread from yielding forever.