So, there is a section in my game where you will be transported to a secondary Place where you will do certain stuff. It will load a different map depending on the “stage” in order to save it using a DataStore.
It loads the current stage both when the Player joins or when the game.ServerScriptService.StageHandler.SaveStage Event is triggered. This is a ServerScript:
game.Players.PlayerAdded:Wait() -- Wait for player to be loaded.
-- Player & Services
local plr = game.Players:GetPlayers()[1] -- It's a singleplayer game.
local gM = require(game.ReplicatedStorage.GameModule)
local TweenService = game:GetService("TweenService")
local CurrentMapFolder = workspace.CurrentMap
function UpdateLighting(LightingName: Model)
for i,v in game.Lighting:GetChildren() do
if not v:IsA("Model") then
v:Destroy()
end
end
for i,v in LightingName:GetChildren() do
local Clone = v:Clone()
v.Parent = game.Lighting
end
end
function CleanWorld()
plr.Character:MoveTo(workspace.DefaultSpawn.Position)
for i,v in CurrentMapFolder:GetChildren() do
v:Destroy()
end
print("Map cleaned up!")
end
gM.SetBlackScreen(true, .001)
do repeat wait() until script.Stage.Value ~= 0 end print("World Stage is updated!")
plr.Character.HumanoidRootPart.Anchored = true
if script.Stage.Value == 1 then
local Map1 = workspace.CurrentMap:FindFirstChild("Map1")
if not Map1 then
Map1 = game.ServerStorage.MAPS.Map1:Clone()
Map1.Parent = workspace.CurrentMap
end
plr.Character:MoveTo(Map1.SpawnLocation.Position)
plr.Character.HumanoidRootPart.Anchored = false
UpdateLighting(game.Lighting.DefaultLighting)
wait(4)
gM.setWalkspeed(11)
-- plr.Character:MoveTo(CurrentMapFolder:WaitForChild("Map1").SpawnLocation.Position)
elseif script.Stage.Value == 2 then
local Map2 = workspace.CurrentMap:FindFirstChild("Map2")
if not Map2 then
Map2 = game.ServerStorage.MAPS.Map2:Clone()
Map2.Parent = workspace.CurrentMap
end
plr.Character:MoveTo(Map2.SpawnLocation.Position)
plr.Character.HumanoidRootPart.Anchored = false
UpdateLighting(game.Lighting.Lighting2)
-- plr.Character:MoveTo(CurrentMapFolder:WaitForChild("Map2").SpawnLocation.Position)
else
error("Invalid Stage loaded!")
end
gM.SetBlackScreen(false, 1)
game.ServerScriptService.StageHandler.SaveStage.Event:Connect(function(stage)
plr.Character.HumanoidRootPart.Anchored = true
if stage == 1 then
local Map1 = workspace.CurrentMap:FindFirstChild("Map1")
if not Map1 then
Map1 = game.ServerStorage.MAPS.Map1:Clone()
Map1.Parent = workspace.CurrentMap
end
plr.Character:MoveTo(Map1.SpawnLocation.Position)
plr.Character.HumanoidRootPart.Anchored = false
UpdateLighting(game.Lighting.DefaultLighting)
wait(4)
gM.setWalkspeed(11)
-- plr.Character:MoveTo(CurrentMapFolder:WaitForChild("Map1").SpawnLocation.Position)
elseif stage == 2 then
CleanWorld()
local Map2 = workspace.CurrentMap:FindFirstChild("Map2")
if not Map2 then
Map2 = game.ServerStorage.MAPS.Map2:Clone()
Map2.Parent = workspace.CurrentMap
end
plr.Character:MoveTo(Map2.SpawnLocation.Position)
plr.Character.HumanoidRootPart.Anchored = false
UpdateLighting(game.Lighting.Lighting2)
-- plr.Character:MoveTo(CurrentMapFolder:WaitForChild("Map2").SpawnLocation.Position)
else
error("Invalid Stage loaded!")
end
print("Loaded Stage: " .. stage)
end)
The issue is, sometimes the Player clips trough the map while the cloning is finishing. It happens sporadically. I tried anchoring the HumanoidRootPart until it “loaded” but it still happens. I don’t know what to do, and the only other solution I have thought of is to host every Stage in a separate Place.
EDIT: Video demonstrating the issue. As you can see it goes trough the map and dies, then spawns up there where a Spawn Location is located.