How to fix scirpt to not be so laggy?

What do I want to achieve?

somehow make this script so that it doesnt lag the entire game by firing remote events

What is the issue?

im currently stumped on how to make it not lag the entire game

What solutions have I tried so far?

Ive looked into adding a wait but it did nothing and I also only know the basics so I am very stupid.

(its a local script)

local RunService = game:GetService("RunService")
local Humanoid = script.Parent:WaitForChild("Humanoid")
local eventidle = game.ReplicatedStorage.Events.Idle
local eventmove = game.ReplicatedStorage.Events.Move


RunService.Heartbeat:Connect(function()
	if Humanoid.MoveDirection.Magnitude > 0.1 then
		print("player is moving")
		eventmove:FireServer()
		wait(0.4)
	else
		print("idle")
		eventidle:FireServer()
		wait(0.4)
	end
end)

2 Likes

You can check those in the server as it’s easily bypassable by an exploiter.

In a server script:

local RunService = game:GetService("RunService")
local players = game.Players

players.PlayerAdded:Connect(function(plr)
	plr.CharacterAdded:Connect(function(char)
		local Humanoid = char:WaitForChild("Humanoid")
		RunService.Heartbeat:Connect(function()
			if Humanoid.MoveDirection.Magnitude > 0.1 then
				print("player is moving")
			else
				print("idle")
			end
		end)
	end)
end)

The reason the wait did nothing is because of threads. Threads are like functions that run on their own. If you wait in a thread, other threads won’t yield. RunService.Heartbeat creates a new thread every frame. Meaning, the previous frame will yield, but the current frame will not since it’s a separate thread.

2 Likes

exotic Use this but dont fireserver

1 Like

Yeah I forgot, I just copy and pasted the whole RunService, and i still forgot to put it

1 Like

But the whole point of the script was to fire a remote event if the player is moving or is idle…

That seems like a werid goal to have. I though the goal was to check if the player was moving?

you can check from the server too, no need for remote events

2 Likes

Completely overcomplicated, just do the checks on the server, it’s more performant and more resistant to exploits.

1 Like

Uhm, you could check if the position of the players character has changed?
I don’t quite know your use case, so this might be wrong.

EDIT: Sorry for tagging the wrong, still new on the forums.

local Players = game:GetService("Players")
local Positions = {}
local CheckInterval = 0.3
local DistanceNeeded = 5

while wait(CheckInterval) do
	for _, Player in pairs(Players:GetPlayers()) do
		local Character = ......-- First check if players character is there and such.
		if Character then
			if Positions[Character.Name] then
				if (Positions[Character.Name] - Character.PrimaryPart.Position).Magnitude > DistanceNeeded then
				-- Do what you want with the player
				end
			else
				Positions[Character.Name] = Character.PrimaryPart.Positition
			end
		end
	end
end

You could even add a ResetInterval, so if X amount of time has passed, then delete the player from the Positions table.

Is lagging because you are not calling the event in the server, this starts to create an event queue so the invocation queue gets exhausted.

local ReplicatedStorage = game:GetService("ReplicatedStorage")

local idle = ReplicatedStorage.Events:WaitForChild("Idle")
local move = ReplicatedStorage.Events:WaitForChild("Move")

idle.OnServerEvent:Connect(function()
	print("Received")
end)                                       -- Use this inside serverscriptservice on a normal script

move.OnServerEvent:Connect(function()
	print("Received")
end)

This is actually going to print a LOT of text in your output, so remove the prints, If you leave the OnServerEvent function empty it will lag again.