Incredibly High Script Rates per Second

Some of my scripts have incredibly high rates in the dev console, and I have determined that servers with high lag have this issue. How does this happen? I don’t believe my scripts have any sort of memory leak (no task.spawn).

What events are triggered within these scripts? I would look at them in the first instance.

Checkpoints:

local checkpointFolder = game.Workspace.Stages
local plrs = game:GetService("Players")
local deathsInARow = 0
local mps = game:GetService("MarketplaceService")
local ids = require(game.ReplicatedStorage.IDS)
game.Players.PlayerAdded:Connect(function(plr)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = plr
	local plrStage = Instance.new("IntValue")
	plrStage.Parent=leaderstats
	plrStage.Name="Stage"
	plrStage.Value=1
	plr.CharacterAdded:Connect(function()
		local stageName = "Stage"..tostring(plrStage.Value)
		local checkpointToSpawn = checkpointFolder[stageName]	
		plr.Character:MoveTo(checkpointToSpawn.Spawn.Position)
		plr.Character.Humanoid.Died:Connect(function()
			deathsInARow += 1
			if deathsInARow >= 3 then
				deathsInARow = 0
				mps:PromptProductPurchase(plr, ids.Skip)
			end
		end)
	end)
	for _, v in checkpointFolder:GetChildren() do
		v.Spawn.Touched:Connect(function(part)
			if part.Parent:FindFirstChild("Humanoid") then
				local touchedPlayer = plrs:GetPlayerFromCharacter(part.Parent)
				if touchedPlayer == plr then
					local stageName =  v.Name
					local stageNum = tonumber(string.sub(stageName,6))
					if stageNum >	 plrStage.Value then
						plrStage.Value = stageNum
					end
				end
			end
		end)
	end
end)

Fade: (46 children)

for _, v in pairs(script.Parent:GetChildren()) do
	if v:IsA("Part") then
		v.Touched:Connect(function(event)
			if event.Parent:FindFirstChild("Humanoid") ~= nil then
				repeat
					v.Transparency = v.Transparency + .1
					wait(.5)
				until v.Transparency >= 1
				v.Transparency = 1
				wait(3)
				repeat
					v.Transparency = v.Transparency - .1
					wait(.5)
				until 0 >= v.Transparency
				v.Transparency = 0
				v.CanCollide = true
			end
			wait(.2)
		end)
	end
end

Gift:

local Tools = game.ReplicatedStorage:WaitForChild("Tools")


local function giftHandler(Player)
	local Gifts = Player:WaitForChild("Gifts")
	for i,v in pairs(Gifts:GetChildren()) do
		if v.Value == true then
			if v.Name == "Gift1" then
				Tools:FindFirstChild("FidgetSpinner"):Clone().Parent = Player.Backpack
			elseif v.Name == "Gift2" then
				Tools:FindFirstChild("BoomBox"):Clone().Parent = Player.Backpack
			elseif v.Name == "Gift3" then
				Tools:FindFirstChild("Gravity Coil"):Clone().Parent = Player.Backpack
			elseif v.Name == "Gift4" then
				Tools:FindFirstChild("Jetpack"):Clone().Parent = Player.Backpack
			elseif v.Name == "Gift5" then
				Tools:FindFirstChild("GrappleHook"):Clone().Parent = Player.Backpack
			elseif v.Name == "Gift6" then
				Tools:FindFirstChild("SpeedCoil"):Clone().Parent = Player.Backpack
			elseif v.Name == "Gift7" then
				Tools:FindFirstChild("SuperSpeedCoil"):Clone().Parent = Player.Backpack
			elseif v.Name == "Gift8" then
				Tools:FindFirstChild("Rainbow"):Clone().Parent = Player.Backpack
			end
		end
	end
end

local function PlayerJoined(Player)
	script.Gifts:Clone().Parent = Player
	local Character = Player.Character or Player.CharacterAdded:Wait()
	giftHandler(Player)
	Player.CharacterAdded:Connect(function(x)
			giftHandler(Player)
	end)
	giftHandler(Player)

		local GiftSecond = Instance.new("NumberValue")
		GiftSecond.Name = "Gift"
		GiftSecond.Parent = Player
		GiftSecond.Value = 0


		while wait(1) do
			GiftSecond.Value += 1
		end	
end

game.Players.PlayerAdded:Connect(function(plr)
	PlayerJoined(plr)
end)

object script is just a tween in like 100 things

this elseif chain hurts my eyes

2 Likes

yeah i didnt make it i bought the game from someone. CBA to fix more of the painful stuff, but trust me, it was wayyy worse.
Example here:
(error in last few lines was because i was in the middle of remaking the entire thing)

:sob: whoever made this game should really learn what a dictionary is

1 Like

A simple solution might be to choose a single script and add a print statement to each Connect to see which is firing the most in each one. Reiterate for each connect under script to determine the cause if it is not obvious from reading them

I just changed it to a module somewhere else but at the time I CBA to fix it there.