Optimization of Exhaustive Scripts

  1. What do you want to achieve? Keep it simple and clear!
    I’m trying to check if someone rolls a specific rarity, by checking as much as possible if a client event has been rolled.
  2. What is the issue? Include screenshots / videos if possible!
    An error on line 4 causes a “Script timeout: exhausted allowed execution time”. Line 4 looks like this:
game.ReplicatedStorage.ForbiddenRolled.OnClientEvent:Connect(function()
  1. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I looked for ways to optimize this script to minimize lag, like using elseif statements, but those ended in errors due to the statements requiring a script, but i HAVE to use a local script.

Full Code:

local message
local prefix = "Server: "
while true do
	game.ReplicatedStorage.ForbiddenRolled.OnClientEvent:Connect(function()
		message = "Someone in the server has rolled a Forbidden!"
		game.TextChatService.TextChannels.RBXGeneral:DisplaySystemMessage(prefix..message)
	end)
	game.ReplicatedStorage.TranscendentRolled.OnClientEvent:Connect(function()
		message = "Someone in the server has rolled a Transcendent!"
		game.TextChatService.TextChannels.RBXGeneral:DisplaySystemMessage(prefix..message)
	end)
	game.ReplicatedStorage.VoidRolled.OnClientEvent:Connect(function()
		message = "Someone in the server has rolled a Void!"
		game.TextChatService.TextChannels.RBXGeneral:DisplaySystemMessage(prefix..message)
	end)
	game.ReplicatedStorage.OmniversalRolled.OnClientEvent:Connect(function()
		message = "Someone in the server has rolled a Omniversal!"
		game.TextChatService.TextChannels.RBXGeneral:DisplaySystemMessage(prefix..message)
	end)
	game.ReplicatedStorage.AscendedRolled.OnClientEvent:Connect(function()
		message = "Someone in the server has rolled a Ascended!"
		game.TextChatService.TextChannels.RBXGeneral:DisplaySystemMessage(prefix..message)
	end)
	game.ReplicatedStorage.ImpossibleRolled.OnClientEvent:Connect(function()
		message = "Someone in the server has rolled a Impossible!"
		game.TextChatService.TextChannels.RBXGeneral:DisplaySystemMessage(prefix..message)
	end)
end

Remove the while loop on line 3.

3 Likes

the main issues I see are

  1. That there is no delay between looping, which is why it’s timing out.

  2. You are looping through code that only really needs to be run once from what I see

Once you :Connect something it doesn’t need to be connected again unless it is disconnected (either manually disconnecting it or destroying the thing that it is connected to)

why are you making the connections every time, only need to do it once

local message
local prefix = "Server: "
game.ReplicatedStorage.ForbiddenRolled.OnClientEvent:Connect(function()
		message = "Someone in the server has rolled a Forbidden!"
		game.TextChatService.TextChannels.RBXGeneral:DisplaySystemMessage(prefix..message)
	end)
	game.ReplicatedStorage.TranscendentRolled.OnClientEvent:Connect(function()
		message = "Someone in the server has rolled a Transcendent!"
		game.TextChatService.TextChannels.RBXGeneral:DisplaySystemMessage(prefix..message)
	end)
	game.ReplicatedStorage.VoidRolled.OnClientEvent:Connect(function()
		message = "Someone in the server has rolled a Void!"
		game.TextChatService.TextChannels.RBXGeneral:DisplaySystemMessage(prefix..message)
	end)
	game.ReplicatedStorage.OmniversalRolled.OnClientEvent:Connect(function()
		message = "Someone in the server has rolled a Omniversal!"
		game.TextChatService.TextChannels.RBXGeneral:DisplaySystemMessage(prefix..message)
	end)
	game.ReplicatedStorage.AscendedRolled.OnClientEvent:Connect(function()
		message = "Someone in the server has rolled a Ascended!"
		game.TextChatService.TextChannels.RBXGeneral:DisplaySystemMessage(prefix..message)
	end)
	game.ReplicatedStorage.ImpossibleRolled.OnClientEvent:Connect(function()
		message = "Someone in the server has rolled a Impossible!"
		game.TextChatService.TextChannels.RBXGeneral:DisplaySystemMessage(prefix..message)
	end)

also all of these could be merged

local Replicated = game:GetService("ReplicatedStorage")

Replicated.Rolled.OnClientEvent:Connect(function(Player, Type)
     game.TextChatService.TextChannels.RBXGeneral:DisplaySystemMessage(`Server: {Player.Name}, has rolled a {Type}`)
end)

Create a remote called Rolle, or anything, just make sure to edit, then when fired, just send the type

Replicated.Rolled:Fire("Void")