How many for i, v in pairs before the server begins to lag?

  1. What do you want to achieve?

Everything is working great besides the lag after a single placement of a plank or door. I can place one door then place one plank, but then I get to the next placement and the lag is heavy when interacting with it.

  1. What is the issue?

As previously stated the lag is very intense when interacting with the items. I have a hammer that must hit the plank so many times to become built, it works well with one item, but once its built and I move on to another, the delay gets worse with every placement, I believe the for i, v in pairs could be the culprit. Or Quite possibly that my custom esk hammer script making it able to interact with clickdetectors could be proving too difficult with all the if statements.

Here is the placeholders I’ve got just for the planks.

  1. What solutions have you tried so far?
    I’ve tried placing the placeholders in replicated storage and pulling from there, but haven’t found a workaround on the click detector function showing the transparency like I’d like. I’ve provided the code below, its quite a lot. But i’m really just wondering if too many for i, v in pairs statements communicating back and forth from local to server scripts could be causing this lag or if it may be the fact that I have a tool with a click detector script that is causing the issues.

Thank you for your time.


---------Local Script----------
for i, plank in pairs(PH:GetChildren()) do
	plank.ClickDetector.MouseHoverEnter:Connect(function()
		plank.Transparency = 0.1
	end)
	plank.ClickDetector.MouseHoverLeave:Connect(function()
		plank.Transparency = 0.5
	end)

	plank.ClickDetector.MouseClick:Connect(function(player)
		if player.leaderstats.Cash.Value >= 10 then
			TargetPlank = plank
			purchaseWindow.Enabled = true	
		else
			local AO = player.PlayerGui.Regular.AO
			AO.Frame.TextLabel.Text = ("No Cash Kill Stuff")
			AO.Enabled = true
			wait(1)
			AO.Enabled = false
		end
	end)	
end	

yes.Activated:Connect(function()
	PP:FireServer(TargetPlank)
	print("Server fired for "..TargetPlank.Name)
	purchaseWindow.Enabled = false
end)

no.Activated:Connect(function()
	purchaseWindow.Enabled = false
end)
-----------Server Script-------------
-- Placing the planks on the window
PP.OnServerEvent:Connect(function(player, plank)
	if player.leaderstats.Cash.Value >= 10 then
		for i, Planks in pairs(PH:GetChildren()) do
			if Planks.Name == plank.Name then
				local newPlank = Plank:Clone()
				newPlank.Name = plank.Name
				newPlank.Position = plank.Position
				newPlank.Orientation = plank.Orientation
				newPlank.Transparency = 0.6
				newPlank.Parent = rPlanks
				plank:Destroy()
			end
		end
		player.leaderstats.Cash.Value = player.leaderstats.Cash.Value - 10

	else
		-- they don't have enough money to buy planks
	end
end)

-- Hammer planks server events

HP.OnServerEvent:Connect(function(player, plank)
	local claim = plank:FindFirstChild("Claim")
	local Sound = game.Workspace.Sounds:WaitForChild("Hammer")
	Sound:Play()
	if plank.Transparency <= 0 and claim.Value == 0 then
		plank.CanCollide = true
		claim.Value = claim.Value + 1
		SB:FireClient(player)
	elseif
		plank.Transparency <=0 and claim.Value == 1 then

	else
		plank.Transparency = plank.Transparency - 0.2  -- Drops Transparency for item

	end 
end)
------Variables and services
local player = game.Players.LocalPlayer
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local PB = ReplicatedStorage.Functions.Notifications:WaitForChild("PlankBuilt")
local DB = ReplicatedStorage.Functions.Notifications:WaitForChild("DoorBuilt")
local HD = ReplicatedStorage.Functions:WaitForChild("HammerDoor")
local HP = ReplicatedStorage.Functions:WaitForChild("HammerPlank")
local Plank = game.Workspace.Base.Windows:WaitForChild("Planks")
local Door = game.Workspace.Base.Doors:WaitForChild("Doors")
local Tool = script.Parent
local Mouse = player:GetMouse()
local debounce = true
local TargetPlank
local TargetDoor


local function PlankBuilt()
	local AO = player.PlayerGui.Regular.AO
	AO.Frame.TextLabel.Text = ("Plank Built")
	AO.Enabled = true
	wait(1)
	AO.Enabled = false	
end

local function DoorBuilt()
	local AO = player.PlayerGui.Regular.AO
	AO.Frame.TextLabel.Text = ("Door Built")
	AO.Enabled = true
	wait(1)
	AO.Enabled = false	
end

Tool.Activated:Connect(function()
	if debounce == true then
		local Animation = Instance.new("Animation", player.Character)
		Animation.AnimationId = "rbxassetid://5900942672"
		local AnimationLoaded = player.Character.Humanoid:LoadAnimation(Animation)
		if Tool.Equipped then
			AnimationLoaded:Play()
			if Mouse.Target and Mouse.Target:FindFirstChild("ClickDetector") then
				if (Tool.Handle.Position - Mouse.Target.Position).Magnitude <= Mouse.Target.ClickDetector.MaxActivationDistance then
					for i, rPlank in pairs(Plank:GetChildren()) do
						if rPlank == Mouse.Target then
							TargetPlank = rPlank
							HP:FireServer(TargetPlank)
							debounce = false
							print("Hammered "..TargetPlank.Name)
						end
						wait(1)
						debounce = true
					end
				end
			end	
		end			
	end	
end)

Tool.Activated:Connect(function()
	if debounce == true then
		local Animation = Instance.new("Animation", player.Character)
		Animation.AnimationId = "rbxassetid://5900942672"
		local AnimationLoaded = player.Character.Humanoid:LoadAnimation(Animation)
		if Tool.Equipped then
			AnimationLoaded:Play()
			if Mouse.Target and Mouse.Target:FindFirstChild("ClickDetector") then
				if (Tool.Handle.Position - Mouse.Target.Position).Magnitude <= Mouse.Target.ClickDetector.MaxActivationDistance then
					for i, rDoor in pairs(Door:GetChildren()) do
						if rDoor == Mouse.Target then
							TargetDoor = rDoor
							HD:FireServer(TargetDoor)
							debounce = false
							print("Hammered "..TargetDoor.Name)
						end
						wait(1)
						debounce = true
					end
				end
			end	
		end			
	end	
end)

PB.OnClientEvent:Connect(PlankBuilt) -- Should display message about the Plank being built
DB.OnClientEvent:Connect(DoorBuilt) -- Should display message about the Plank being built
1 Like