Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Is there a way to make this copy script run faster without lagging the server?

I'm making a randomly generated Area by going through each location, and then recalculating chance. I'm using game:GetService("RunService").Heartbeat:wait() to slow down the script so it doesn't do it all at once and lag the server.

local player = game.Players.LocalPlayer
local rebirths = player.leaderstats.Rebirths.Value
local button = script.Parent
local Prefabs = game.ReplicatedStorage.Prefabs
local cubes = 90
local width = 100
local length = 50
local height = 6
local X = 0
local Y = 0
local Z = 0
local rows = 1
local w = 0
local l = 0
local h = 0
local chance = 0
math.randomseed(tick())

function DrawBlock(block, xp, yp, zp, location, num)
	if num == "" then
		local newBlock = Prefabs[block]:Clone()
		newBlock.Parent = location
		newBlock.Size = Vector3.new(3,3,3)
		newBlock.CFrame = CFrame.new(xp,yp,zp)
   else
		local newBlock = Prefabs[block]:Clone()
		newBlock.Parent = location
		newBlock.CFrame = CFrame.new(xp,yp,zp)
		newBlock.Size = Vector3.new(3,3,3)
	end
end

button.MouseButton1Click:connect(function()
	if player.TeamColor == BrickColor.new("Really blue") then
		local X = 148.95
		local Y = -102
		local Z = 615.84
while h ~= height do
		chance = math.random(100)/100
			if l == length then
				w = 0
				l = 0
				Z = 615.84
				X = 148.95
				Y = Y-3
				h = h+1
			elseif w == width then
				w = 0
				Z = Z-3
				X = 148.95
				l = l+1
			elseif chance<1 and chance>.85 then
				DrawBlock("Iron",X,Y,Z,workspace.ReallyBlueBase,"")
				w = w+1
				X = X-3

			elseif chance<.84 and chance>.74 then
				DrawBlock("Coal",X,Y,Z,workspace.ReallyBlueBase,"")
				w = w+1
				X = X-3
			else
				DrawBlock("Stone",X,Y,Z,workspace.ReallyBlueBase,"")
				w = w+1
				X = X-3
			end
	game:GetService("RunService").Heartbeat:wait()
		end
	end
end)

Thanks in advance!

1 comment

0

Try putting wait(timehere) in the script above and tell me what you get. Do it at the main parts of where it sends and changes these colors.

1 answer

✓ Accepted answer

You should always parent the part at the end. What is happening in Your script, is that the engine renders each part three times:

  • Default size at default location
  • New size at default location
  • New size at new location
     local newBlock = Prefabs[block]:Clone()
        newBlock.Parent = location
        newBlock.Size = Vector3.new(3,3,3)
        newBlock.CFrame = CFrame.new(xp,yp,zp)

By parenting the part last, engine will have to render the part only once, where it should be and with the right size. There are also lots of different things going on behind the scenes, lagging things further. There was even an article in dev forums, encouraging the practice.

Long story short, you should see a MASSIVE improvement just by swapping these lines.

     local newBlock = Prefabs[block]:Clone()
        newBlock.Size = Vector3.new(3,3,3)
        newBlock.CFrame = CFrame.new(xp,yp,zp)
        newBlock.Parent = location

This discussion is preserved as it was. New answers, comments, and votes are closed.