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,
Can’t you use Attributes to store properties of the map Lighting instead of Basevalues?
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
Some of the WaitForChilds can be removed, such as the ones for Storage for example, but that’s up to you