I want to make an obby game where there are different stages. Each stage is in a different place because I want to change game settings for each one. There are buttons with the level names on the front. I want it so that a player has to finish the previous stage in order to be able to press the next button, so that they will go to the next stage. (The first button just teleports the player right away)
The problem is that I am having trouble figuring out how to transfer data between the places.
I tried following video tutorials on YouTube but none really fitted what I was trying to accomplish, so I tried scripting it on my own using various sites for certain parts of the code.
This is what I have:
Script for the spawning place:
--DataStore variables
local DataStoreService = game:GetService("DataStoreService")
local LevelsBeatenStore = DataStoreService:GetDataStore("LevelsBeatenStore")
local BeatenStore = DataStoreService:GetDataStore("BeatenStore")
local Stage = DataStoreService:GetDataStore("Stage")
game.Players.PlayerAdded:Connect(function(player)
--Creates variables
LevelsBeaten = {}
local Beaten = false
local StageNum = 1
local data
--Loads the table of levels beaten
local success, errormessage = pcall(function()
data = LevelsBeatenStore:GetAsync(player.UserId.."-LevelsBeaten")
end)
if success then
print("Data successfully loaded")
LevelsBeaten = data
print(LevelsBeaten)
else
print("There was an error loading data")
warn(errormessage)
end
--Loads if they beat the stage or not
local success, errormessage = pcall(function()
data = BeatenStore:GetAsync(player.UserId.."-Beaten")
end)
if success then
print("Data successfully loaded")
Beaten = data
print(Beaten)
else
print("There was an error loading data")
warn(errormessage)
end
--Loads the stage number
local success, errormessage = pcall(function()
data = Stage:GetAsync(player.UserId.."-Stage")
end)
if success then
print("Data successfully loaded")
StageNum = data
print(StageNum)
else
print("There was an error loading data")
warn(errormessage)
end
if LevelsBeaten == nil then
LevelsBeaten = {Beaten}
else
table.insert(LevelsBeaten,StageNum,Beaten)
end
print(LevelsBeaten)
end)
game.Players.PlayerRemoving:Connect(function(player)
local success, errormessage = pcall(function()
LevelsBeatenStore:SetAsync(player.UserId.."-LevelsBeaten",LevelsBeaten)
end)
if success then
print("Data successfully saved")
else
print("There was an error saving data")
warn(errormessage)
end
local success, errormessage = pcall(function()
LevelsBeatenStore:SetAsync(player.UserId.."-Beaten",BeatenStore)
end)
if success then
print("Data successfully saved")
else
print("There was an error saving data")
warn(errormessage)
end
local success, errormessage = pcall(function()
LevelsBeatenStore:SetAsync(player.UserId.."-Stage",Stage)
end)
if success then
print("Data successfully saved")
else
print("There was an error saving data")
warn(errormessage)
end
end)
game.ReplicatedStorage.Event.Event:Connect(function()
game.ReplicatedStorage.LevelBeaten:Fire(LevelsBeaten)
end)
Script for the button:
script.Parent.ClickDetector.MouseClick:Connect(function(player)
game.ReplicatedStorage.Event:Fire()
game.ReplicatedStorage.LevelBeaten.Event:Connect(function(LevelsBeaten)
if LevelsBeaten[1] == true then
game:GetService("TeleportService"):Teleport(5882129816, player)
else
print("Has not beaten the previous level")
end
end)
end)
Script for the first stage:
local DataStoreService = game:GetService("DataStoreService")
local BeatenStore = DataStoreService:GetDataStore("BeatenStore")
local Stage = DataStoreService:GetDataStore("Stage")
game.Players.PlayerAdded:Connect(function(player)
Beaten = game.ReplicatedStorage.Beaten
StageNum = game.ReplicatedStorage.Stage
local data
local success, errormessage = pcall(function()
data = BeatenStore:GetAsync(player.UserId.."-Beaten")
end)
if success then
print("Data successfully loaded")
Beaten = data
else
print("There was an error loading data")
warn(errormessage)
end
end)
game.Players.PlayerRemoving:Connect(function(player)
if Beaten.Value == false then
local success, errormessage = pcall(function()
BeatenStore:SetAsync(player.UserId.."-Beaten",Beaten.Value)
end)
if success then
print("Data successfully saved")
else
print("There was an error saving data")
warn(errormessage)
end
end
local success, errormessage = pcall(function()
Stage:SetAsync(player.UserId.."-Stage",StageNum.Value)
end)
if success then
print("Data successfully saved")
else
print("There was an error saving data")
warn(errormessage)
end
end)
It’s a little long but that’s mainly because I don’t really know what I’m doing and most of this is new to me. (Things I don’t understand too well: DataStores, RemoteFunctions, and Tables)