Script Rate Very High

Hello Developers,
I’m working on a slapping game and I’ve ran into a major problem. I’m not sure if it spam lags my client or others but. My -GSyncServer script has a very high Run Rate and I’m not sure what I should do about it cause Im clueless on why it’s so high.

How could I fix this problem?

  • Code
-- Services --
local PlrService = game:GetService('Players')
local ReplicatedStorage = game:GetService('ReplicatedStorage')
-- Constants / Remotes --
local GameRemotes = ReplicatedStorage:FindFirstChild('GameRems')
local MainEvent = GameRemotes:FindFirstChild('MainEvent')
----

-- OnServerEvent --
MainEvent.OnServerEvent:Connect(function(LocalPlr,GivenEvent,CurrentSlapper,FollowPart)
	local Character = LocalPlr.Character
	local Head = Character:FindFirstChild('Head')
	local SlapperHumanoid = CurrentSlapper.Humanoid
	-- Check x2 --
	if GivenEvent == 'Replicate' then
		if not Character:FindFirstChild('FollowPart') then
			-- Create New Part --
			local Part = Instance.new('Part')
			-- Setup Part --
			Part.Name = 'FollowPart'
			Part.Parent = CurrentSlapper
			Part.Size = Vector3.new(1,1,1)
			Part.Transparency = 1
			Part.Anchored = true
			Part.CanCollide = false
			Part.Position = FollowPart
			-- Setup IKControls --
			SlapperHumanoid.ArmControl.Target = Part
			SlapperHumanoid.TorsoControl.Target = Part
			task.wait(0.25)
			SlapperHumanoid.TorsoControl.Weight = 1
			SlapperHumanoid.ArmControl.Weight = 1
		else
			local Part = CurrentSlapper:WaitForChild('FollowPart')
			-- Updating --
			Part.Position = FollowPart
		end
		-- Check --
	elseif GivenEvent == 'EndReplicate' then
		-- Setup IKControls --
		SlapperHumanoid.ArmControl.Weight = 0
		SlapperHumanoid.TorsoControl.Weight = 0
		task.wait(0.01)
		-- Check --
		if Character:FindFirstChild('FollowPart') then
			Character.FollowPart:Destroy()
		end
	end
end)
----
1 Like

I don’t see anything that could be wrong with your server code. When does your client code fire the MainEvent remote event?

Maybe you can try to use debounces on the client?

If your firing a lot of events in a short amount of time and/or sending a lot of data then you will experience lag. I suspect that’s what the issue is as I don’t see anything wrong with the code. How often are you firing the event?

1 Like

Sure, you could try throttling the event and debouncing. You could also try to cut down on how many times you use FindFirstChild because iterating through the hierarchy can be fairly resource-intensive. You could also use Region3 objects and methods for collision detection, which can be more efficient than individual part checks. Provided below is potentially more optimized code.

local PlrService = game:GetService('Players')
local ReplicatedStorage = game:GetService('ReplicatedStorage')

local GameRemotes = ReplicatedStorage:FindFirstChild('GameRems')
local MainEvent = GameRemotes:FindFirstChild('MainEvent')

MainEvent.OnServerEvent:Connect(function(LocalPlr, GivenEvent, CurrentSlapper, FollowPart)
    local Character = LocalPlr.Character
    local Head = Character:FindFirstChild('Head')
    local SlapperHumanoid = CurrentSlapper.Humanoid
    local FollowPartObject = Character:FindFirstChild('FollowPart')

    if GivenEvent == 'Replicate' then
        if not FollowPartObject then
            local Part = Instance.new('Part')
            Part.Name = 'FollowPart'
            Part.Parent = CurrentSlapper
            Part.Size = Vector3.new(1, 1, 1)
            Part.Transparency = 1
            Part.Anchored = true
            Part.CanCollide = false
            Part.Position = FollowPart

            SlapperHumanoid.ArmControl.Target = Part
            SlapperHumanoid.TorsoControl.Target = Part

            task.wait(0.25)
            SlapperHumanoid.TorsoControl.Weight = 1
            SlapperHumanoid.ArmControl.Weight = 1
        else
            FollowPartObject.Position = FollowPart
        end
    elseif GivenEvent == 'EndReplicate' then
        SlapperHumanoid.ArmControl.Weight = 0
        SlapperHumanoid.TorsoControl.Weight = 0
        task.wait(0.01)
        if FollowPartObject then
            FollowPartObject:Destroy()
        end
    end
end)

hope this helps 11 !!! 1!!

I was gonna ask. How could I do this so it’s strictly only clients could see it?

Like the server cannot see it only clients can

The code structure looks generally correct, but there are a few potential issues that you might want to address:

  1. Error Handling: Always include error handling in your code, especially when dealing with remote events and instances. This ensures that your code can gracefully handle unexpected situations.
  2. Naming Conventions: It’s a good practice to use consistent naming conventions for your variables. For example, you start with CamelCase for some variables (e.g., GivenEvent), but then use PascalCase for others (e.g., SlapperHumanoid). Choose one convention and stick to it for clarity.
  3. Instance Checking: When checking if an instance exists, you can use the :FindFirstChild method, but it’s better to use the :FindFirstChild method with a specific name, if possible, to avoid issues if other unrelated instances with similar names exist in the hierarchy.

Here’s an updated version of your code with these considerations in mind:

-- Services --
local Players = game:GetService('Players')
local ReplicatedStorage = game:GetService('ReplicatedStorage')

-- Constants / Remotes --
local GameRemotes = ReplicatedStorage:FindFirstChild('GameRems')
local MainEvent = GameRemotes:FindFirstChild('MainEvent')

-- OnServerEvent --
MainEvent.OnServerEvent:Connect(function(LocalPlr, GivenEvent, CurrentSlapper, FollowPart)
    local Character = LocalPlr.Character
    local SlapperHumanoid = CurrentSlapper:FindFirstChildOfClass('Humanoid')
    
    if not Character or not SlapperHumanoid then
        return -- Error handling: Ensure that Character and SlapperHumanoid exist.
    end
    
    if GivenEvent == 'Replicate' then
        if not Character:FindFirstChild('FollowPart') then
            -- Create New Part --
            local Part = Instance.new('Part')
            -- Setup Part --
            Part.Name = 'FollowPart'
            Part.Parent = CurrentSlapper
            Part.Size = Vector3.new(1, 1, 1)
            Part.Transparency = 1
            Part.Anchored = true
            Part.CanCollide = false
            Part.Position = FollowPart
            -- Setup IKControls --
            SlapperHumanoid.ArmControl.Target = Part
            SlapperHumanoid.TorsoControl.Target = Part
            task.wait(0.25)
            SlapperHumanoid.TorsoControl.Weight = 1
            SlapperHumanoid.ArmControl.Weight = 1
        else
            local Part = Character:FindFirstChild('FollowPart')
            -- Updating --
            Part.Position = FollowPart
        end
    elseif GivenEvent == 'EndReplicate' then
        -- Setup IKControls --
        SlapperHumanoid.ArmControl.Weight = 0
        SlapperHumanoid.TorsoControl.Weight = 0
        task.wait(0.01)
        -- Check --
        local Part = Character:FindFirstChild('FollowPart')
        if Part then
            Part:Destroy()
        end
    end
end)

This updated code includes better error handling and consistent naming conventions, making it more robust and easier to understand. Make sure to adapt it further based on your specific needs and debugging requirements.

1 Like

I’ve also ran into another problem with the actual In Gameplay script. Sometimes it would automatically slap the player without even checking the box to see if you’ve readied up to slap. Do you think you can take a look at the code and see what’s going on?

Idk if you can tell but. To be able to slap the oppoenet first you gotta put your mouse/finger on the right side of the screen confirming the slap before any other action is played. But it somehow seems to bypass it and I’m not sure how

@NewRidStudios

local CanTouchDeb = true
  • Function
local function NewStartGame()
	-- Check Update --
	CameraFolder.Parent:GetAttributeChangedSignal('GameStarted'):Connect(function()
		if CameraFolder.Parent:GetAttribute('GameStarted',true) then
			-- Setup Game --
			local Char1 = workspace:FindFirstChild(QueuingFolder.TeamBlue:GetAttribute('QueuedPlr'))
			local Char2 = workspace:FindFirstChild(QueuingFolder.TeamRed:GetAttribute('QueuedPlr'))
			-- Constants / Camera --
			local IntroCam1 = CameraFolder:FindFirstChild('IntroCam1')
			local IntroCam2 = CameraFolder:FindFirstChild('IntroCam2')
			local RedCamera = CameraFolder:FindFirstChild('RedCamera')
			local BlueCamera = CameraFolder:FindFirstChild('BlueCamera')
			-- Call Events --
			task.wait(0.2)
			MainEvent:FireClient(PlrService:GetPlayerFromCharacter(Char1),{IntroCam1,BlueCamera},'To IntroCamera')
			MainEvent:FireClient(PlrService:GetPlayerFromCharacter(Char2),{IntroCam2,RedCamera},'To IntroCamera')
			QueueEvent:FireClient(PlrService:GetPlayerFromCharacter(Char1),'HideLeave')
			QueueEvent:FireClient(PlrService:GetPlayerFromCharacter(Char2),'HideLeave')
			-- Teleport --
			Char1.HumanoidRootPart.CFrame = CFrame.new(FloorModel.Blue.Position.X,Char1.HumanoidRootPart.Position.Y,FloorModel.Blue.Position.Z)
			Char1.HumanoidRootPart.Orientation = Vector3.new(0,180,0)
			Char2.HumanoidRootPart.CFrame = CFrame.new(FloorModel.Red.Position.X,Char2.HumanoidRootPart.Position.Y,FloorModel.Red.Position.Z)
			Char2.HumanoidRootPart.Orientation = Vector3.new(0,0,0)
			-- Create Stuff --
			Char1:SetAttribute('PartSide','ToRed')
			Char2:SetAttribute('PartSide','ToBlue')
			-- Functions --
			local function SetupGame()
				-- Health / Attribute --
				Char1:SetAttribute('Health',100)
				Char2:SetAttribute('Health',100)
				-- Make It Pads Not Getonable --
				local RedPad = QueuingFolder:FindFirstChild('TeamRed')
				local BluePad = QueuingFolder:FindFirstChild('TeamBlue')
				-- Update --
				RedPad:SetAttribute('Queueable',false)
				BluePad:SetAttribute('Queueable',false)
			end
			-- Call Functons --
			SetupGame()
			task.wait(6)
			--EndGame()
			-- Now We Start --
			MainEvent:FireClient(PlrService:GetPlayerFromCharacter(Char1),'Slapping')
			QueueEvent:FireClient(PlrService:GetPlayerFromCharacter(Char1),'ShowInstructor')
			MainEvent:FireClient(PlrService:GetPlayerFromCharacter(Char2),'NotSlapping')
			-- Show Health --
			QueueEvent:FireClient(PlrService:GetPlayerFromCharacter(Char1),'ShowHealth')
			QueueEvent:FireClient(PlrService:GetPlayerFromCharacter(Char2),'ShowHealth')
			-- UI Principal --
			QueueEvent:FireClient(PlrService:GetPlayerFromCharacter(Char1),'ShowClocker')
			QueueEvent:FireClient(PlrService:GetPlayerFromCharacter(Char2),'HideClocker')
			-- Enable Scripts --
			Char2.Hitboxes.HeadBox['-Server'].Enabled = true
			Char2.Humanoid.HeadControl.Enabled = true
			-- On Touched / Character 2 --
			Char2.Hitboxes.HeadBox.Touched:Connect(function(BasePart)
				-- Check x2 --
				if BasePart.Parent.Parent:FindFirstChild('Humanoid') and BasePart.Parent.Parent:IsA('Model') and BasePart.Name == 'HandBox' then
					if CanTouchDeb == false then
						-- Update --
						CanTouchDeb = true
						MainEvent:FireClient(PlrService:GetPlayerFromCharacter(BasePart.Parent.Parent),'Touched Part')
						--MainEvent:FireClient(PlrService:GetPlayerFromCharacter(Char2),'GotSlapped')
						--MainEvent:FireClient(PlrService:GetPlayerFromCharacter(Char1),'JustSlapped')
						-- Update Current Slapper --
						MainEvent:FireClient(PlrService:GetPlayerFromCharacter(Char2),'Slapping')
						MainEvent:FireClient(PlrService:GetPlayerFromCharacter(Char1),'NotSlapping')
						-- UI Principal --
						QueueEvent:FireClient(PlrService:GetPlayerFromCharacter(Char2),'ShowClocker')
						QueueEvent:FireClient(PlrService:GetPlayerFromCharacter(Char1),'HideClocker')
						QueueEvent:FireClient(PlrService:GetPlayerFromCharacter(Char2),'ShowInstructor')
						-- Update --
						Char2.Hitboxes.HeadBox['-Server'].Enabled = false
						Char2.Humanoid.HeadControl.Enabled = false
						-- Update --
						Char1.Hitboxes.HeadBox['-Server'].Enabled = true
						Char1.Humanoid.HeadControl.Enabled = true
					end
				end
			end)
			-- On Touched / Character 2 --
			Char1.Hitboxes.HeadBox.Touched:Connect(function(BasePart)
				-- Check x2 --
				if BasePart.Parent.Parent:FindFirstChild('Humanoid') and BasePart.Parent.Parent:IsA('Model') and BasePart.Name == 'HandBox' then
					if CanTouchDeb == false then
						-- Update --
						CanTouchDeb = true
						MainEvent:FireClient(PlrService:GetPlayerFromCharacter(BasePart.Parent.Parent),'Touched Part')
						--MainEvent:FireClient(PlrService:GetPlayerFromCharacter(Char1),'GotSlapped')
						--MainEvent:FireClient(PlrService:GetPlayerFromCharacter(Char2),'JustSlapped')
						-- Update Current Slapper --
						MainEvent:FireClient(PlrService:GetPlayerFromCharacter(Char1),'Slapping')
						MainEvent:FireClient(PlrService:GetPlayerFromCharacter(Char2),'NotSlapping')
						-- UI Principal --
						QueueEvent:FireClient(PlrService:GetPlayerFromCharacter(Char1),'ShowClocker')
						QueueEvent:FireClient(PlrService:GetPlayerFromCharacter(Char2),'HideClocker')
						QueueEvent:FireClient(PlrService:GetPlayerFromCharacter(Char1),'ShowInstructor')
						-- Update --
						Char1.Hitboxes.HeadBox['-Server'].Enabled = false
						Char1.Humanoid.HeadControl.Enabled = false
						-- Update --
						Char2.Hitboxes.HeadBox['-Server'].Enabled = true
						Char2.Humanoid.HeadControl.Enabled = true
					end
				end
			end)
		end
	end)
	-- Pads / Constants --
	local BluePad = QueuingFolder:FindFirstChild('TeamBlue')
	local RedPad = QueuingFolder:FindFirstChild('TeamRed')
	-- Check Changed / Blue --
	BluePad:GetAttributeChangedSignal('QueuedPlr'):Connect(function()
		if BluePad:GetAttribute('QueuedPlr') ~= '' and RedPad:GetAttribute('QueuedPlr') ~= '' then
			-- Update --
			CameraFolder.Parent:SetAttribute('GameStarted',true)
		end
		-- Check --
		if RedPad:GetAttribute('QueuedPlr') == '' then
			CameraFolder.Parent:SetAttribute('GameStarted',false)
		end
	end)
	-- Check Changed / Red --
	RedPad:GetAttributeChangedSignal('QueuedPlr'):Connect(function()
		if BluePad:GetAttribute('QueuedPlr') ~= '' and RedPad:GetAttribute('QueuedPlr') ~= '' then
			-- Update --
			CameraFolder.Parent:SetAttribute('GameStarted',true)
		end
		-- Check --
		if BluePad:GetAttribute('QueuedPlr') == '' then
			CameraFolder.Parent:SetAttribute('GameStarted',false)
		end
	end)
end
----
  • What Should Be Sent (But I guess can be bypassed
-- OnServerEvent --
QueueEvent.OnServerEvent:Connect(function(LocalPlr,SentEvent)
	-- Check --
	if SentEvent == 'ReadyToSlap' then
		task.wait(0.05)
		CanTouchDeb = false
	end
end)
----
  • Inside The Client Script
-- Mouse Stuff With Box --
ConfirmBox.MouseEnter:Connect(function()
	TweenService:Create(ConfirmBox.Arrow,TweenInfo.new(0.5,Enum.EasingStyle.Quint,Enum.EasingDirection.Out),{TextTransparency = 1}):Play()
	TweenService:Create(ConfirmBox.Context,TweenInfo.new(0.5,Enum.EasingStyle.Quint,Enum.EasingDirection.Out),{TextTransparency = 1}):Play()
	TweenService:Create(ConfirmBox.Arrow.TextStroke,TweenInfo.new(0.5,Enum.EasingStyle.Quint,Enum.EasingDirection.Out),{Transparency = 1}):Play()
	TweenService:Create(ConfirmBox.Context.TextStroke,TweenInfo.new(0.5,Enum.EasingStyle.Quint,Enum.EasingDirection.Out),{Transparency = 1}):Play()
	task.wait(1)
	ConfirmBox.Visible = false
	-- Fire To Server --
	RemoteEvent:FireServer('ReadyToSlap')
end)
----
1 Like

I feel like some of this code can be better optimized but idk :person_shrugging: