Hi, im a new user of roblox studio. so be patient with a noob.
Basically, I want to optimize my script as much as I can.
2.I don’t see a way to optimize my script any further.
The script I want to optimize:
local Sound1 = script.Parent.TimeSound
local Sound2 = script.Parent.Sound2
local s = script.Stat
local vals = game.ReplicatedStorage.vals
t = 0
while true do
t = 30
repeat
t = t-1
s.Value = "Intermission... "..t
wait(1)
until t == 0
s.Value = "Game starting!"
wait(2)
blocco.CanCollide = true
local mapselect = game.ReplicatedStorage.Games:GetChildren()
local choose = math.random(1,#mapselect)
curnum = 0
for i =1,#mapselect do
curnum = curnum +1
if curnum == choose then
mapselect[i]:Clone().Parent = workspace
curmap = mapselect[i].Name
s.Value = "We'll be playing "..mapselect[i].Name
end
end
wait(3)
local plrs = game.Players:GetChildren()
for i = 1,#plrs do
local num = math.random(1,32)
plrs[i].Character.Head.CFrame = CFrame.new(workspace.Teleports["Part"..num].Position)
plrs[i].Character.Parent = workspace.Ingame
end
t = 4
repeat
t = t-1
if t == 3 then
s.Value = "Ready"
end
if t == 2 then
s.Value = "Steady"
end
if t == 1 then
s.Value = "Set"
end
if t == 0 then
s.Value = "Go!"
end
if s.Value == "Go!" then
blocco.CanCollide = false
end
if s.Value == "Ready" then
Sound2:Play()
end
wait(1)
until t == 0
t=120
repeat
t = t-1
s.Value = t.." seconds left"
if t == 10 then
Sound1:Play()
end
wait(1)
until t ==0 or vals.Winner.Value ~= "" or #workspace.Ingame:GetChildren() == 0
if vals.Winner.Value ~= "" then
s.Value = vals.Winner.Value.. " has won!"
game.Players[vals.Winner.Value].leaderstats.Wins.Value = game.Players[vals.Winner.Value].leaderstats.Wins.Value +1
vals.Winner.Value = ""
if #workspace.Ingame:GetChildren() == 0 then
s.Value = "No one has won!"
end
else
s.Value = "No one has won!"
end
if vals.Winner.Value =="" then
Sound1:Stop()
end
wait(3)
local ingame = workspace.Ingame:GetChildren()
for i =1,#ingame do
local plr = game.Players:GetPlayerFromCharacter(ingame[i])
plr:LoadCharacter()
end
workspace[curmap]:Destroy()
end
Screen. As you can see, script is put in Workspace
Thank you for putting the time into reading this and helping out!
Time performance i guess. I’m asking this because I have noticed that if the game graphics are high the player sometimes doesn’t get teleported to the map
That doesn’t sound like a performance issue, that sounds like theres a bug. In that case you would want maintainability. One thing you can do is reduce repeated code so that if theres a bug in that code, it isn’t duplicated and thus you don’t end up having to fix and test it twice. You have a couple countdowns that do the same thing. They start from some number, count down, and update s.Value each time the count changes. You could move this to a function.
There are a couple rules about your game that I can guess at based on this code:
When the game is “Running” there must be a map loaded
A map must not be loaded when the game is in “Intermission”
These are good places to have checks. In particular you dont want to teleport players until the map is done loading for them, I predict that may be the cause of players sometimes not teleporting.
I tidied your script up a bit and adjusted for some possible errors, it would be helpful if you could tell me all the problems with your script right now so I can fix it better.
Code:
--//Services
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
--//Variables
local vals = ReplicatedStorage.vals
local Sound1 = script.Parent.TimeSound
local Sound2 = script.Parent.Sound2
local s = script.Stat
--//Controls
local timeRemaining = 0
--//Tables
local timeValues = {
[3] = "Ready",
[2] = "Steady",
[1] = "Set",
[0] = "Go",
}
--//Loops
while true do
--//Intermission
timeRemaining = 30
repeat
timeRemaining -= 1
s.Value = "Intermission... ".. timeRemaining
task.wait(1)
until timeRemaining == 0
s.Value = "Game starting!"
task.wait(2)
blocco.CanCollide = true
local mapselect = ReplicatedStorage.Games:GetChildren()
local choose = Random.new():NextInteger(1, #mapselect)
local curnum = 0
for i = 1, #mapselect do
curnum += 1
if curnum == choose then
mapselect[i]:Clone().Parent = workspace
curmap = mapselect[i].Name
s.Value = "We'll be playing "..mapselect[i].Name
end
end
task.wait(3)
--//Start game
local plrs = Players:GetPlayers()
for i = 1, #plrs do
local num = Random.new():NextInteger(1, 32)
plrs[i].Character.Head.CFrame = CFrame.new(workspace.Teleports["Part".. num].Position)
plrs[i].Character.Parent = workspace.Ingame
end
timeRemaining = 4
repeat
timeRemaining -= 1
s.Value = timeValues[timeRemaining]
if s.Value == "Go!" then
blocco.CanCollide = false
end
if s.Value == "Ready" then
Sound2:Play()
end
task.wait(1)
until timeRemaining == 0
timeRemaining = 120
repeat
timeRemaining -= 1
s.Value = timeRemaining.." seconds left"
if timeRemaining == 10 then
Sound1:Play()
end
task.wait(1)
until timeRemaining == 0 or vals.Winner.Value ~= "" or #workspace.Ingame:GetChildren() == 0
--//End game
if vals.Winner.Value ~= "" then
s.Value = vals.Winner.Value.. " has won!"
Players[vals.Winner.Value].leaderstats.Wins.Value = game.Players[vals.Winner.Value].leaderstats.Wins.Value +1
vals.Winner.Value = ""
if #workspace.Ingame:GetChildren() == 0 then
s.Value = "No one has won!"
end
else
s.Value = "No one has won!"
end
if vals.Winner.Value =="" then
Sound1:Stop()
end
task.wait(3)
local ingame = workspace.Ingame:GetChildren()
for i = 1, #ingame do
local player = Players:GetPlayerFromCharacter(ingame[i])
pcall(player.LoadCharacter, player)
end
workspace[curmap]:Destroy()
end
I recently tested the game and when I turn up the graphics 1 out of 3 times the player doesn’t teleport to the map. I don’t see anything wrong with the output.
--//Services
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
--//Variables
local vals = ReplicatedStorage.vals
local Sound1 = script.Parent.TimeSound
local Sound2 = script.Parent.Sound2
local s = script.Stat
--//Controls
local timeRemaining = 0
--//Tables
local timeValues = {
[3] = "Ready",
[2] = "Steady",
[1] = "Set",
[0] = "Go",
}
--//Loops
while true do
--//Intermission
timeRemaining = 30
repeat
timeRemaining -= 1
s.Value = "Intermission... ".. timeRemaining
task.wait(1)
until timeRemaining == 0
s.Value = "Game starting!"
task.wait(2)
blocco.CanCollide = true
local mapselect = ReplicatedStorage.Games:GetChildren()
local choose = Random.new():NextInteger(1, #mapselect)
local curnum = 0
for i = 1, #mapselect do
curnum += 1
if curnum == choose then
mapselect[i]:Clone().Parent = workspace
curmap = mapselect[i].Name
s.Value = "We'll be playing ".. mapselect[i].Name
end
end
task.wait(3)
--//Start game
for i, player in ipairs(Players:GetPlayers()) do
local character = player.Character
local head = character and character:FindFirstChild("Head")
print(head)
if head then
print("teleported")
head:PivotTo(CFrame.new(workspace.Teleports:WaitForChild("Part".. Random.new():NextInteger(1, 32)).Position))
character.Parent = workspace.Ingame
end
end
timeRemaining = 4
repeat
timeRemaining -= 1
s.Value = timeValues[timeRemaining]
if s.Value == "Go!" then
blocco.CanCollide = false
end
if s.Value == "Ready" then
Sound2:Play()
end
task.wait(1)
until timeRemaining == 0
timeRemaining = 120
repeat
timeRemaining -= 1
s.Value = timeRemaining.." seconds left"
if timeRemaining == 10 then
Sound1:Play()
end
task.wait(1)
until timeRemaining == 0 or vals.Winner.Value ~= "" or #workspace.Ingame:GetChildren() == 0
--//End game
if vals.Winner.Value ~= "" then
s.Value = vals.Winner.Value.. " has won!"
Players[vals.Winner.Value].leaderstats.Wins.Value = Players[vals.Winner.Value].leaderstats.Wins.Value +1
vals.Winner.Value = ""
if #workspace.Ingame:GetChildren() == 0 then
s.Value = "No one has won!"
end
else
s.Value = "No one has won!"
end
if vals.Winner.Value == "" then
Sound1:Stop()
end
task.wait(3)
local ingame = workspace.Ingame:GetChildren()
for i = 1, #ingame do
local player = Players:GetPlayerFromCharacter(ingame[i])
pcall(player.LoadCharacter, player)
end
workspace[curmap]:Destroy()
end
Tell me what it prints when you’re in high graphics.
however the first script works much better and in the 10 tests I’ve done I’ve always been teleported to the map. the only problem is that the part called ‘block’ seems to have the cancolide always true
Could you also include the part where you reference the blocco part? In your script you never referenced it in a variable so that could be the problem.
Yep you’re right. Despite I included the blocco part in the variables in your script as “local blocco = script.Parent.BloccoPartenza” cancollide remains true
--//Services
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
--//Variables
local vals = ReplicatedStorage.vals
local Sound1 = script.Parent.TimeSound
local Sound2 = script.Parent.Sound2
local s = script.Stat
local blocco = script.Parent.BloccoPartenza
--//Controls
local timeRemaining = 0
--//Tables
local timeValues = {
[3] = "Ready",
[2] = "Steady",
[1] = "Set",
[0] = "Go",
}
--//Loops
while true do
--//Intermission
timeRemaining = 30
repeat
timeRemaining -= 1
s.Value = "Intermission... ".. timeRemaining
task.wait(1)
until timeRemaining == 0
s.Value = "Game starting!"
task.wait(2)
blocco.CanCollide = true
local mapselect = ReplicatedStorage.Games:GetChildren()
local choose = Random.new():NextInteger(1, #mapselect)
local curnum = 0
for i = 1, #mapselect do
curnum += 1
if curnum == choose then
mapselect[i]:Clone().Parent = workspace
curmap = mapselect[i].Name
s.Value = "We'll be playing "..mapselect[i].Name
end
end
task.wait(3)
--//Start game
local plrs = Players:GetPlayers()
for i = 1, #plrs do
local num = Random.new():NextInteger(1, 32)
plrs[i].Character.Head.CFrame = CFrame.new(workspace.Teleports["Part".. num].Position)
plrs[i].Character.Parent = workspace.Ingame
end
timeRemaining = 4
repeat
timeRemaining -= 1
s.Value = timeValues[timeRemaining]
if s.Value == "Go!" then
blocco.CanCollide = false
end
if s.Value == "Ready" then
Sound2:Play()
end
task.wait(1)
until timeRemaining == 0
timeRemaining = 120
repeat
timeRemaining -= 1
s.Value = timeRemaining.." seconds left"
if timeRemaining == 10 then
Sound1:Play()
end
task.wait(1)
until timeRemaining == 0 or vals.Winner.Value ~= "" or #workspace.Ingame:GetChildren() == 0
--//End game
if vals.Winner.Value ~= "" then
s.Value = vals.Winner.Value.. " has won!"
Players[vals.Winner.Value].leaderstats.Wins.Value = game.Players[vals.Winner.Value].leaderstats.Wins.Value +1
vals.Winner.Value = ""
if #workspace.Ingame:GetChildren() == 0 then
s.Value = "No one has won!"
end
else
s.Value = "No one has won!"
end
if vals.Winner.Value =="" then
Sound1:Stop()
end
task.wait(3)
local ingame = workspace.Ingame:GetChildren()
for i = 1, #ingame do
local player = Players:GetPlayerFromCharacter(ingame[i])
pcall(player.LoadCharacter, player)
end
workspace[curmap]:Destroy()
end
Could you try this? I reload the player before teleporting them to ensure they exist.
New code:
--//Services
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
--//Variables
local vals = ReplicatedStorage.vals
local Sound1 = script.Parent.TimeSound
local Sound2 = script.Parent.Sound2
local s = script.Stat
local blocco = script.Parent.BloccoPartenza
--//Controls
local timeRemaining = 0
--//Tables
local timeValues = {
[3] = "Ready",
[2] = "Steady",
[1] = "Set",
[0] = "Go",
}
--//Loops
while true do
--//Intermission
timeRemaining = 30
repeat
timeRemaining -= 1
s.Value = "Intermission... ".. timeRemaining
task.wait(1)
until timeRemaining == 0
s.Value = "Game starting!"
task.wait(2)
blocco.CanCollide = true
local mapselect = ReplicatedStorage.Games:GetChildren()
local choose = Random.new():NextInteger(1, #mapselect)
local curnum = 0
for i = 1, #mapselect do
curnum += 1
if curnum == choose then
mapselect[i]:Clone().Parent = workspace
curmap = mapselect[i].Name
s.Value = "We'll be playing "..mapselect[i].Name
end
end
task.wait(3)
--//Start game
local plrs = Players:GetPlayers()
for i, player in ipairs(Players:GetPlayers()) do
pcall(player.LoadCharacter, player)
task.defer(function()
local randomNumber = Random.new():NextInteger(1, 32)
player.Character.Head.CFrame = CFrame.new(workspace.Teleports["Part".. randomNumber].Position)
player.Character.Parent = workspace.Ingame
end)
end
timeRemaining = 4
repeat
timeRemaining -= 1
s.Value = timeValues[timeRemaining]
if s.Value == "Go!" then
blocco.CanCollide = false
end
if s.Value == "Ready" then
Sound2:Play()
end
task.wait(1)
until timeRemaining == 0
timeRemaining = 120
repeat
timeRemaining -= 1
s.Value = timeRemaining.." seconds left"
if timeRemaining == 10 then
Sound1:Play()
end
task.wait(1)
until timeRemaining == 0 or vals.Winner.Value ~= "" or #workspace.Ingame:GetChildren() == 0
--//End game
if vals.Winner.Value ~= "" then
s.Value = vals.Winner.Value.. " has won!"
Players[vals.Winner.Value].leaderstats.Wins.Value = game.Players[vals.Winner.Value].leaderstats.Wins.Value +1
vals.Winner.Value = ""
if #workspace.Ingame:GetChildren() == 0 then
s.Value = "No one has won!"
end
else
s.Value = "No one has won!"
end
if vals.Winner.Value =="" then
Sound1:Stop()
end
task.wait(3)
local ingame = workspace.Ingame:GetChildren()
for i = 1, #ingame do
local player = Players:GetPlayerFromCharacter(ingame[i])
pcall(player.LoadCharacter, player)
end
workspace[curmap]:Destroy()
end
local mapselect = ReplicatedStorage.Games:GetChildren()
local choose = Random.new():NextInteger(1, #mapselect)
local curnum = 0
for i = 1, #mapselect do
curnum += 1
if curnum == choose then
mapselect[i]:Clone().Parent = workspace
curmap = mapselect[i].Name
s.Value = "We'll be playing "..mapselect[i].Name
end
end
To this:
local mapselect = ReplicatedStorage.Games:GetChildren()
local choose = Random.new():NextInteger(1, #mapselect)
mapselect[choose]:Clone().Parent = workspace
curmap = mapselect[choose].Name
s.Value = "We'll be playing "..curmap