(I don’t know if this should be in development discussion instead, so please tell me if I need to change it)
In a game I’m making I need the screen to fade in and out when the player teleports to another location. I want to know the most efficient way to do this. Should I make it run a function every time? Should I use a remote event (though I think that would make it happen on the server…) What should I do?
Efficient in terms of what? How easy it is to program? How easy it is to use in the future? How fast in runs on peoples’ PCs?
The last one doesn’t really matter, because all the ways there are of doing it are fast enough, and even with lots of other scripts running, the fade animation is only going to be running very rarely, i.e. when the player is teleporting.
As for the two others, I think the easiest is to define a function that you always use in all your other scripts to teleport players. That way you only need to logic for running the fade animation in one place, and you can do everything related to teleporting with a single function call.
So it depends on what your use case is and under what conditions a players should be teleported. If it’s e.g. a GUI where there’s a teleport button, it’s probably fine if it’s called on the client without involving the server. But if it’s to e.g. teleport a player when they are killed in the game, or it costs in-game currency to teleport, it should be done on the server for the same reasons that any other game-critical stuff / stuff that can affect other players should be done on the server.
For simplicity I’d do it all on the server, and have a RemoteEvent that a LocalScript can Fire if it needs to make the client player teleport.
What I mean by efficient is if I need to write out the whole thing for every script it is in or if there is a better way to do it. And also it is only teleporting in the same game to a different location.
I am confused. If you’re using TeleportService to teleport a player to another place in the same game, and you want the screen to fade in and out without the loading screen, I am pretty sure there’s an option for that in TeleportService. I’ve seen story games like daycare 2 using that option. But if you want to teleport them simply to another position in your game, and you don’t want to write out the whole thing, I would recommend a for loop. For example:
lets say you have a black screen you want to fade in, and the black screen is simply a black frame. What I would do is add a function that runs when the player chooses to teleport. This is entirely your choosing. The only thing is that when the player chooses, this function in a localscript in the frame runs:
function random()
for i = 1,0,-0.05 do
wait(0.05)
script.Parent.Parent.BackgroundTransparency = i --the for loop loops through the transparency, and makes it little less transparent each time, creating the fade in effect
end
wait()
script.Parent.Parent.BackgroundTransparency = 0
wait(2)
for i = 0,1,0.05 do
wait(0.05)
script.Parent.Parent.BackgroundTransparency = i --same thing here, except it makes it fade out.
end
end
The most efficient way then is to have a function somewhere that can be called from all the other scripts. That way, any time the player needs to do the whole teleport, fade-in/fade-out thing, it’s just a one-line call to that function instead of typing out the same code over and over again, or copy/pasting for that matter. It’s also super important to program in this way in general, because you’ll often end up changing the way that things happen later on, e.g. making the fade animation shorter or longer or adding effects. Being able to change code in a single place instead of potentially 100s is great.
I don’t understand what exactly you want on the logic side, but on visual:
TweenService can animate any number value, Vector2 or Vector3.
I usually call them this way
local TS = game:GetService("TweenService")
local TI1 = TweenInfo.new(<TimeDuration>, Enum.EasingStyle, Enum.EasingDirection)
I suggest putting any TweenInfos that are used more than once (and don’t need different time values) outside of whatever function or loop you’re using.
For calling the animation, do the following (this is supposed to be in the function you want the animation to be started from):
local Anim = TS:Create(<instance>, TI1, {Goal})
Anim:Play()