Script being super slow when not intended to

Hey everyone! My script is being super slow when I transferred it to another game, anyone know how to fix this?
What should happen: https://gyazo.com/87b56171ce98f9cec2b5b26590c13266
What actually happens in the transferred game: https://gyazo.com/1eb3b9210261a96e2166cb6605992acf

local groupId = 123456
local adminRank = 9 
local timer = 3 -
local typespeed = 0.05 

local textlabel = game.Workspace.Rulesboard.RulesBoard.SurfaceGui.Rules
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local remoteEvent = ReplicatedStorage:WaitForChild("RulesEvent")
local remoteEvent2 = ReplicatedStorage:WaitForChild("RulesEvent2")
local function typewrite(object,text)
	for i = 1,#text,1 do
		object.Text = string.sub(text,1,i)
		wait(typespeed)
	end
end

function onChatted(msg, speaker)
	 if speaker:GetRankInGroup(groupId) >= adminRank then
	local source = string.lower(speaker.Name)
	msg = string.lower(msg)
		if string.find(msg, "!rules") then
			remoteEvent:FireAllClients()
			wait(1)
			typewrite(textlabel,"Hey there! Welcome to Embassy's Training Centre. Before we begin, there are some rules you must be aware of.") 
			wait(timer)
			typewrite(textlabel,"Please ensure that you are listening to your trainer's instructions through out the session.")		
			wait(timer)
			typewrite(textlabel,"If you're planning to idle or afk, notify your trainer. You do not want to be falsely removed from the training session.")	
			wait(timer)
			typewrite(textlabel,"Please make sure that you use flawless grammar to the best of your potential, as it is important in getting good results.")
			wait(timer)
			typewrite(textlabel,"If you have any questions, feel free to ask a trainer. Do not ask the host or co-host.")	
			wait(timer)
			typewrite(textlabel,"Do not leave until you have been kicked by a high rank. Otherwise, you may not receive your rank.")	
			wait(timer)
			typewrite(textlabel,"If you are seen roaming around freely without permission, you will be removed from the training session.")	
			wait(timer)
			typewrite(textlabel,"We are now prepared. Please follow your respective trainers into the training rooms. Good luck trainees!")	
			wait(timer)
			remoteEvent2:FireAllClients()
			else
		end
end
end
game.Players.PlayerAdded:Connect(function(player)
	player.Chatted:connect(function(msg)onChatted(msg, player) end)
end)

Please help me, all help is appreciated.

you didn’t use deltatime which means if your internet is slow, the text typing will be slow

Using Deltatime

I didn’t quite understand what I have to do with Deltatime, do I have to change the loop to a different one, how would I have to modify it?

you’re most likely running into a memory leak because of the constant wait() spam on the server side, a far more efficient method would be to make the entire rule function handled by the client after the RulesEvent remote is fired

It’s meant for all users as the RemoteEvent is fired on all clients. Would this affect it?

you can simply move the entire rule listing function into the remote’s OnClientEvent function, which I assume is the function responsible for making the surfacegui visible & it should work as intended

1 Like

It seems to be working, but the camera isn’t positioned. I believe it runs the first RemoteEvent and then right after the second one that takes it off. Do you know how this could be fixable?
https://gyazo.com/4e4e759b9f988e380d014c278ba4eb5b

can you send me the localscripts for the 2 events?

First:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local remoteEvent = ReplicatedStorage:WaitForChild("RulesEvent")


local function onRulesCmd()
	local textlabel = game.Workspace.Rulesboard.RulesBoard.RulesGUI.Rules
	local timer = 3 -- Seconds until slide changes
	local typespeed = 0.05 -- The speed the typing is done at
	local function typewrite(object,text)
		for i = 1,#text,1 do
			object.Text = string.sub(text,1,i)
			wait(typespeed)
		end
	end
	workspace.CurrentCamera.CameraType = Enum.CameraType.Scriptable
	workspace.CurrentCamera.CameraSubject = workspace.Rulesboard.CameraPart
	workspace.CurrentCamera.CFrame = workspace.Rulesboard.CameraPart.CFrame
	wait(1)
	typewrite(textlabel,"Hey there! Welcome to Embassy's Training Centre. Before we begin, there are some rules you must be aware of.") 
	wait(timer)
	typewrite(textlabel,"Please ensure that you are listening to your trainer's instructions through out the session.")		
	wait(timer)
	typewrite(textlabel,"If you're planning to idle or afk, notify your trainer. You do not want to be falsely removed from the training session.")	
	wait(timer)
	typewrite(textlabel,"Please make sure that you use flawless grammar to the best of your potential, as it is important in getting good results.")
	wait(timer)
	typewrite(textlabel,"If you have any questions, feel free to ask a trainer. Do not ask the host or co-host.")	
	wait(timer)
	typewrite(textlabel,"Do not leave until you have been kicked by a high rank. Otherwise, you may not receive your rank.")	
	wait(timer)
	typewrite(textlabel,"If you are seen roaming around freely without permission, you will be removed from the training session.")	
	wait(timer)
	typewrite(textlabel,"We are now prepared. Please follow your respective trainers into the training rooms. Good luck trainees!")	
	wait(timer)
	typewrite(textlabel," ")	
end

remoteEvent.OnClientEvent:Connect(onRulesCmd)

Second:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local remoteEvent = ReplicatedStorage:WaitForChild("RulesEvent2")

local function offRulesCmd()
	workspace.CurrentCamera.CameraType = Enum.CameraType.Custom
	workspace.CurrentCamera.CameraSubject = game.Players.LocalPlayer.Character.Humanoid
end

remoteEvent.OnClientEvent:Connect(offRulesCmd)

you can remove the second event and move the offRulesCmd function to the end of the first event’s script

1 Like