Possible to make the game manager more reliable?

Hello! I am wondering if I could make this code a lot more reliable.

This code is used for setting up the game. It does work, but the only thing is, it sometimes doesn’t. The sometimes is pretty rare though.

local repstor = game:GetService("ReplicatedStorage")
local storage = game:GetService("ServerStorage")
local done = script:GetAttribute("Done")

game:GetService("ReplicatedStorage"):WaitForChild("GetLocalMap").OnServerEvent:Connect(function(plr, map)
	if storage.PlayerLate.Value == false then
		wait(5)
		if done == false then
			done = true
			print(map)
			if not game:GetService("Workspace"):FindFirstChild("Map") then
				local modelmap = storage:WaitForChild("Maps"):FindFirstChild(map):Clone()
				modelmap.Name = "Map"
				modelmap.Parent = game:GetService("Workspace")
			end
			if storage:WaitForChild("Music"):WaitForChild(map).Value then
				plr.PlayerGui:WaitForChild("MapMusic"):WaitForChild("Music").SoundId = "rbxassetid://"..storage:WaitForChild("Music"):FindFirstChild(map).Value
			end

			local mapLight = storage:WaitForChild("Lighting"):FindFirstChild(map)
			local mapLightAt = mapLight:WaitForChild("Atmosphere"):Clone()
			mapLightAt.Parent = game:GetService("Lighting")
			local mapLightSky = mapLight:WaitForChild("Skybox"):Clone()
			mapLightSky.Parent = game:GetService("Lighting")
			game.Lighting.Ambient = mapLight.Ambient.Value
			game.Lighting.ExposureCompensation = mapLight.ExposureCompensation.Value
			game.Lighting.ShadowSoftness = mapLight.ShadowSoftness.Value
			game.Lighting.EnvironmentDiffuseScale = mapLight.EnvitomentDiffuseScale.Value
			game.Lighting.Brightness = mapLight.Brightness.Value
		else
			if storage:WaitForChild("Music"):WaitForChild(map).Value then
				plr.PlayerGui:WaitForChild("MapMusic"):WaitForChild("Music").SoundId = "rbxassetid://"..storage:WaitForChild("Music"):FindFirstChild(map).Value
			end
		end
		game.ReplicatedStorage.LoadKarts:Invoke(plr)
		if "done" then
			wait(4)
			game.Workspace:WaitForChild("Map"):WaitForChild("Players")[plr.Team.Name]:WaitForChild("DriveSeat"):Sit(plr.Character.Humanoid)
			repstor:WaitForChild("GameIsLoaded"):FireAllClients()
			storage.PlayerLate.Value = true
			repstor:WaitForChild("LoadReady").Value = true
		end

	else
		plr.PlayerGui:WaitForChild("MapMusic"):WaitForChild("Music").SoundId = "rbxassetid://"..storage:WaitForChild("Music"):FindFirstChild(map).Value
		game.ReplicatedStorage.LoadKarts:Invoke(plr)
		if "done" then
			wait(4)
			game.Workspace:WaitForChild("Map"):WaitForChild("Players")[plr.Team.Name]:WaitForChild("DriveSeat"):Sit(plr.Character.Humanoid)
		end	
	end
	repstor:WaitForChild("LoadReady").Value = true
	repstor:WaitForChild("GameIsLoaded"):FireClient(plr)

end)		

If you could help make this code more reliable, thanks. Have questions about the code? Feel free to ask them! This code was 100% written myself back in Feburary.

There’s a bit of repetition and confusing bits such as

if "done" then

conditions that are always going to return true regardless of the actual value of the Done attribute, so it could be false yet that condition would happen since if a string atleast has something in it, it’s true, else it’s false. This is the most I believe could be done

local repstor = game:GetService("ReplicatedStorage")
local storage = game:GetService("ServerStorage")
local done = script:GetAttribute("Done")

local loadedEvent = repstor:WaitForChild("GameIsLoaded")
local loadReady = repstor:WaitForChild("LoadReady")

local function setMusic(plr,map)
	plr.PlayerGui:WaitForChild("MapMusic"):WaitForChild("Music").SoundId = "rbxassetid://"..storage:WaitForChild("Music"):FindFirstChild(map).Value
end

local function seatPlayer(plr)
	workspace:WaitForChild("Map"):WaitForChild("Players")[plr.Team.Name]:WaitForChild("DriveSeat"):Sit(plr.Character.Humanoid)
end

game:GetService("ReplicatedStorage"):WaitForChild("GetLocalMap").OnServerEvent:Connect(function(plr, map)
	if not storage.PlayerLate.Value  then
		wait(5)
		if not done then
			done = true
			print(map)
			if not workspace:FindFirstChild("Map") then
				local modelmap = storage:WaitForChild("Maps"):FindFirstChild(map):Clone()
				modelmap.Name = "Map"
				modelmap.Parent = workspace
			end
			
			if storage:WaitForChild("Music"):WaitForChild(map).Value then
				setMusic(plr,map)
			end
			
			local lighting = game:GetService("Lighting")
			local mapLight = storage:WaitForChild("Lighting"):FindFirstChild(map)
			local mapLightAt = mapLight:WaitForChild("Atmosphere"):Clone()
			mapLightAt.Parent = lighting
			local mapLightSky = mapLight:WaitForChild("Skybox"):Clone()
			mapLightSky.Parent = lighting
			lighting.Ambient = mapLight.Ambient.Value
			lighting.ExposureCompensation = mapLight.ExposureCompensation.Value
			lighting.ShadowSoftness = mapLight.ShadowSoftness.Value
			lighting.EnvironmentDiffuseScale = mapLight.EnvitomentDiffuseScale.Value
			lighting.Brightness = mapLight.Brightness.Value
		else
			if storage:WaitForChild("Music"):WaitForChild(map).Value then
				setMusic(plr,map)
			end
		end
		game.ReplicatedStorage.LoadKarts:Invoke(plr)
		if done then
			wait(4)
			
			seatPlayer(plr)
			loadedEvent:FireAllClients()
			storage.PlayerLate.Value = true
			loadReady.Value = true
		end

	else
		setMusic(plr,map)
		game.ReplicatedStorage.LoadKarts:Invoke(plr)
		if done then
			wait(4)
			seatPlayer(plr)
		end	
	end
	loadReady.Value = true
	loadedEvent:FireClient(plr)

end)		

Also a few things,

  1. Can’t you use Attributes to store properties of the map Lighting instead of Basevalues?
  2. I’m not sure that in done = true, you meant to also set the Attribute to true as well since that’s only changing it for the variable, not the attribute, so even whe nyou start over, it’s going to be true always, since nothing sets it to false
  3. Some of the WaitForChilds can be removed, such as the ones for Storage for example, but that’s up to you

Overall, I’m not sure what else could be done

1 Like

Yea, I will use Attributes for everything. This was written while Attributes were in beta.

Then I think once you do that, and do some changes of yourself, this should help it be 100% reliable

1 Like