Updating cframe lags whole server [URGENT]

Hello
Im trying to make a climbing system using cframes and doing so im changing them on the client.
To replicate it to the server i constantly update it to the server, making everything lag like crazy.
Do you know a better way to do so?

My local script:
hS is basically a variable if the player is holding the S key

	while hS do
		local ray3 = workspace:Raycast(hrp.Position + Vector3.new(0, -.6, 0), hrp.CFrame.LookVector * 8, params2)
			if not ray3 then -- onder
				stop(nil, nil, "Stop")
					immobilize()
					print("Onder")
					return
				end
					canManoeuvre(-hrp.CFrame.UpVector)
					idleAnim()
					hrp.CFrame *= CFrame.new(0, -upDownIncrements, 0)
					update() -- we look at this
					task.wait()
				end

In the update() function i constantly send out a remote event

local function update()
	remote:FireServer("Update", hrp.CFrame)
end

And in the server i do this:

if arg == "Update" then
		hrp.CFrame = CF
	end

Singleplayer: https://gyazo.com/cb925ef4b51e830a1a3360ef4d17c343]
With another player: https://gyazo.com/baf42eed9726edae3c56b95170c33a0b
Is there a better way to do this?

  1. In your local script, create a debounce variable to track the time since the last update:
local lastUpdate = tick() -- Initialize to current time

local function debounceUpdate()
    local currentTime = tick()
    if currentTime - lastUpdate >= 0.1 then -- Adjust the debounce delay as needed
        lastUpdate = currentTime
        remote:FireServer("Update", hrp.CFrame)
    end
end
  1. Modify your update() function to call the debounceUpdate() function instead of directly sending the update to the server:
local function update()
    debounceUpdate()
end
  1. On the server, handle the updates as before:
if arg == "Update" then
    hrp.CFrame = CF
end

wont this make everything less smooth?

Try using Cframe interpolation

what’s that? do you have an article on that or can you explain in short?

Smooth movements basically in a 3D environment without any weird stutter or anything

Between positions too that can help

well, sounds good to me. how do implement that?
Server or client?

  1. Modify the local script:

    • Add a variable to store the previous CFrame value:

      local previousCFrame = hrp.CFrame
      
    • Replace the line hrp.CFrame *= CFrame.new(0, -upDownIncrements, 0) with the following code to perform CFrame interpolation:

      local targetCFrame = hrp.CFrame * CFrame.new(0, -upDownIncrements, 0)
      local interpolationFactor = 0.1 -- Adjust this value for desired smoothness
      hrp.CFrame = previousCFrame:Lerp(targetCFrame, interpolationFactor)
      previousCFrame = hrp.CFrame
      
    • Remove the update() function and the line remote:FireServer("Update", hrp.CFrame).

  2. Modify the server-side code:

    • Remove the code block:
      if arg == "Update" then
        hrp.CFrame = CF
      end
      

well, doesn’t the server need to know when to update the CFrame to the Humanoidrootpart? because other players need to see the player climbing too

You know what here is the ai lol

  1. Local Script:

    • Replace the line hrp.CFrame *= CFrame.new(0, -upDownIncrements, 0) with the following code:
      local targetCFrame = hrp.CFrame * CFrame.new(0, -upDownIncrements, 0)
      local interpolationFactor = 0.1 -- Adjust this value for desired smoothness
      hrp.CFrame = previousCFrame:Lerp(targetCFrame, interpolationFactor)
      previousCFrame = hrp.CFrame
      
      -- Send the updated CFrame to the server
      remote:FireServer("UpdateCFrame", hrp.CFrame)
      
  2. Server Script:

    • Modify the server-side code to handle the updated CFrame:
      remote.OnServerEvent:Connect(function(player, arg, newCFrame)
        if arg == "UpdateCFrame" then
          -- Update the player's CFrame on the server
          hrp.CFrame = newCFrame
      
          -- Broadcast the updated CFrame to all other players
          for _, otherPlayer in ipairs(game.Players:GetPlayers()) do
            if otherPlayer ~= player then
              remote:FireClient(otherPlayer, "UpdateCFrame", player, newCFrame)
            end
          end
        end
      end)
      
      -- Add a listener for when players join the game
      game.Players.PlayerAdded:Connect(function(player)
        -- Send the current player's CFrame to the newly joined player
        remote:FireClient(player, "UpdateCFrame", player, hrp.CFrame)
      end)
      
  3. Client Script:

    • Add the following code to handle the updated CFrame from other players:
      remote.OnClientEvent:Connect(function(player, arg, newCFrame)
        if arg == "UpdateCFrame" then
          -- Update the CFrame of the specified player on the client
          local character = player.Character
          if character then
            local hrp = character:FindFirstChild("HumanoidRootPart")
            if hrp then
              hrp.CFrame = newCFrame
            end
          end
        end
      end)
      

With these modifications, hopefully it helps you know

oh sorry for asking for help, didn’t know it was so hard for you. Thank you, I will try it out tomorrow

Yea no I just use the AI to go around devforum so

but hopefully that helps if there’s error you might need to debugging

this may or may not be a dumb question but won’t this lag too as we are still send like 200+ remote event requests?

You’re right, sending a large number of remote event requests can potentially cause lag. To optimize the performance, you can implement a throttling mechanism to limit the frequency of updates being sent.

Here’s a modified version of the code that incorporates a throttling mechanism:

  1. Local Script:

    • Replace the line remote:FireServer("UpdateCFrame", hrp.CFrame) with the following code:
      local targetCFrame = hrp.CFrame * CFrame.new(0, -upDownIncrements, 0)
      local interpolationFactor = 0.1 -- Adjust this value for desired smoothness
      hrp.CFrame = previousCFrame:Lerp(targetCFrame, interpolationFactor)
      previousCFrame = hrp.CFrame
      
      -- Check if enough time has passed since the last update
      local currentTime = tick()
      if currentTime - lastUpdateTime >= updateInterval then
        -- Send the updated CFrame to the server
        remote:FireServer("UpdateCFrame", hrp.CFrame)
        lastUpdateTime = currentTime
      end
      
  2. Server Script:

    • Add the following variables at the top of the script:

      local updateInterval = 0.1 -- Adjust this value for desired update frequency (in seconds)
      local lastUpdateTimes = {} -- Stores the last update time for each player
      
    • Modify the server-side code to handle the updated CFrame:

      remote.OnServerEvent:Connect(function(player, arg, newCFrame)
        if arg == "UpdateCFrame" then
          -- Check if enough time has passed since the last update for this player
          local currentTime = tick()
          local lastUpdateTime = lastUpdateTimes[player]
          if not lastUpdateTime or currentTime - lastUpdateTime >= updateInterval then
            -- Update the player's CFrame on the server
            hrp.CFrame = newCFrame
      
            -- Broadcast the updated CFrame to all other players
            for _, otherPlayer in ipairs(game.Players:GetPlayers()) do
              if otherPlayer ~= player then
                remote:FireClient(otherPlayer, "UpdateCFrame", player, newCFrame)
              end
            end
      
            -- Update the last update time for this player
            lastUpdateTimes[player] = currentTime
          end
        end
      end)
      

bro, I’m assuming you are using an Ai or some shi. I can literally tell you did not write this, if you don’t know, just say and leave I created this topic for people who may know a solution or who can bring me a step forward. This is not it

he was saying he been using ai past 10 minute how is you not noticed.

your code has updating a lot very frequeast and it
s hard to see what you trting do maybe you can simply the code.

yes I noticed but I thought maybe it worked idk why I didn’t tell earlier.

What I’ve said is that I’m sending too many requests to the server and that causes lag, maybe someone knew a work around to this

1 Like

he sort of did say he was using ai…

Personally, I don’t think it’s really a good idea to be using AI on devforum posts… the point(in my opinion) is for people who see a post on a subject they understand to #1 explain what they did wrong and #2 show them how to fix it