Hello you absolute legends who are looking for tutorials! I'm Mark_GoodMan, a vietnamese teenager who makes tutorial for all of you! Today we will be learning about ReplicatedStorage and how it works.
Now i have found a few questions:
Q. What is ReplicatedStorage and what does it do?
A. ReplicatedStorage is used to store stuff and objects, but it gets replicated to the server or the client too, anything can be stored inside until needed, let's say you have bunch of models and building in Workspace, but there's no more space to hold these building, so what you need to do is put all of them inside ReplicatedStorage and it will stores there and free up space for Workspace, you can stores many objects in there until needed.
Q. Is it important to make games?
A. Depends, if you want to make a game where the map will change every 5-10 minutes, then ReplicatedStorage is what you needed for it! Most of games that has shop will contains at least every objects inside the ReplicatedStorage, so when you buy something in the shop, it will move the object from ReplicatedStorage to the player, or Workspace.
Now let's have an example of how ReplicatedStorage actually works!
Now the first thing we want to do is make an example model like the image below:
I will use 3 models as example, but this is optional, you can make as many models as you want! But 3 is already enough!
Rename them to anything you want, now drag all of them into ReplicatedStorage REMEMBER IF YOU RENAMED THEM TO ANYTHING, MAKE SURE TO PUT 1 OR 2 OR 3 ON THEM AND CHANGE THE NAME IN THE SCRIPT UNDER
After dragging all of them into ReplicatedStorage, now the maps in Workspace is gone
That's because those 3 models are stored inside ReplicatedStorage, and that means they moved to another kind of Workspace, but they still stay in the same position, no matter if you drag them out or drag in, it's still the same position as you originally set them to, the thing is it still appears those move, rotate and scale arrow in Workspace when you selected them from ReplicatedStorage like the image (but you can't move or rotate them anyway), making them look like they are invisible.
After that, now everything is set, let's move to scripting!
Now after the setup is done, now we want to load each map from ReplicatedStorage to Workspace.
All you need to do is insert either Script, or Localscript, it doesn't matter, use Script if you want everyone to see the same map loaded, use Localscript if you want each player to see different map loaded
Insert 1 of these script in StarterPlayer >>> StarterPlayerScripts I will use Localscript, but it's your choice to choose Script or Localscript
Now let's start scripting:
If you know the game called catalog heaven, you noticed every 5 minutes, the map changes to a random map, this means every 5 minutes, the current map will move to ReplicatedStorage if it's currently in Workspace, and it will pick another random map and replace it from ReplicatedStorage to Workspace, and repeat.
So what we need to do is make a script for it, but this time i set 3 seconds so you can test faster!
First, we want to do this on loop, and then we make a math so it selects randomly!
while true do
local number = math.random(1,3) -- this is optional, if you have 7 maps, then put it as (1,7)
Now of course we want to start loading maps, using FindFirstChild, once it finds the map that has a name of "Example", it will choose a number on a random model that has same name, but different number, once the map is selected with the correct number, it will move the selected map to Workspace using parent. Remember to use Wait(), or else the game will lag a lot.
while true do
local number = math.random(1,3) -- this is optional, if you have 7 maps, then put it as (1,7)
wait(3)
game.ReplicatedStorage:FindFirstChild("Example"..number).Parent = workspace
Now you want to unload (or you can call it remove) the map from workspace, all you need to do is use FindFirstChild on workspace to find a model called "Example 1,2,3" and then it will attempt to move the loaded map back to ReplicatedStorage, and then wait 3 seconds again and new map loaded, and then repeat the script on loop.
while true do
local number = math.random(1,3)
wait(3)
game.ReplicatedStorage:FindFirstChild("Example"..number).Parent = workspace
wait(3)
workspace:FindFirstChild("Example"..number).Parent = game.ReplicatedStorage
end
And that's the entire script, now you can test, here's the result: When a map gets loaded and moved to Workspace, it's no longer in ReplicatedStorage and it's in Workspace instead, and after 3 second it will no longer be in Workspace, but it moves back to ReplicatedStorage instead, and then wait 3 second and a random map will be chosen and will be moved to Workspace then moves back on loop.
Example 2 is loaded and then 3 seconds later it's moved back to ReplicatedStorage and the other Example loaded
Now this is important for loading games, such large games will require this to reduce lag when loading the game, as there are many large object loading at same time will cause lag for players, so we want to load objects slowly.
Let's make the script for this one (in the same Script or Localscript at StarterPlayer >>> StarterPlayerScript)
First we want to get ReplicatedStorage service and then a function
local rs = game:GetService("ReplicatedStorage")
function getmap(number)
Now we will use FindFirstChild function to find the Example with numbers in ReplicatedStorage, if it found map, then it will print "Found map!", and it will return the script for later
local rs = game:GetService("ReplicatedStorage")
function getmap(number)
if rs:FindFirstChild("Example"..number) then
print("Found map!")
return rs:FindFirstChild("Example"..number)
end
end
The next thing to do is we want to use repeat function, because when every object is loaded, the script will stop so it won't run. We will then use wait() because if you don't, it will lag a lot, now we will use math.random to select random map to load, if findmap found an Example, print "Loaded Example 1,2,3", then it will move the map to workspace, and it will repeat until there's no more objects called "Example" in ReplicatedStorage, and it will stop the code from running.
local rs = game:GetService("ReplicatedStorage")
function getmap(number)
if rs:FindFirstChild("Example"..number) then
print("Found map!")
return rs:FindFirstChild("Example"..number)
end
end
repeat
wait(3)
local findmap = getmap(math.random(1,3))
if findmap ~= nil then
print("Loaded " .. findmap.Name)
findmap.Parent = workspace
end
until rs:FindFirstChild("")
Now let's test this!
When it's loading the map, it will print that a map is found and loaded successfully from ReplicatedStorage to Workspace, and once 3 Example (objects) loaded into Workspace, no more prints, which mean the code stopped running because there's no more objects in ReplicatedStorage.
This year, i have lots of plans of making tutorial for you guys, and i would love to see your feedback, review this tutorial and give it a 5 stars, because it took me 7 hours to make this tutorial! Thank you so much for supporting me over the last few months.
You also learned how to use ReplicatedStorage, i included everything in the tutorial, if you didn't know anything, review the tutorial again, because i'm sure you can do it :D
I will try my best to make all of you achieve successful for your game and goodluck!
This tutorial originally created on 7/7/2020, as it may gets updated over-time for bugs fixing and improvements!
Have a nice day my friend, thank you for reading the tutorial.
Last time checked this tutorial was 7/7/2020