Script failing to follow mouse

Hey, my script is really laggy / the model is really laggy when I am moving the part which is really annoying, lol.
I’ve tried many steps, so basically, there is a model containing a handpart which gets CFramed to the lefthand, but then there’s a hitpart that I need it to follow the mouse without it laggy and stuff.

Server

game.ReplicatedStorage.BeamAbility.OnServerEvent:Connect(function(plr, mouse, state)
	
	if state == "clone" then
		local Model = script.Beam:Clone()
		Model.Parent = plr.Character.LeftHand
		Model.HandPart:SetNetworkOwner(plr)
		
	
		Model.HandPart.CFrame = plr.Character.LeftHand.CFrame
		Model.HandPart.Anchored = true
		
	elseif state == "followMouse" then
		local Model = plr.Character.LeftHand:WaitForChild("Beam")
		
		repeat wait()
			Model.HitPart.Position = mouse
		until state == "finished"
	end
end)

Client:

local UIS = game:GetService("UserInputService")
local DefaultFoV = 70
local plr = game.Players.LocalPlayer

local mouse = plr:GetMouse()

local Properties = {FieldOfView = DefaultFoV - 40}
local Info = TweenInfo.new(0.5, Enum.EasingStyle.Sine, Enum.EasingDirection.InOut) -- edit as you want
local FOVt = game:GetService("TweenService"):Create(game.Workspace.CurrentCamera, Info, Properties)

local Properties2 = {FieldOfView = DefaultFoV}
local Info2 = TweenInfo.new(0.5, Enum.EasingStyle.Sine, Enum.EasingDirection.InOut) -- edit as you want
local FOVt2 = game:GetService("TweenService"):Create(game.Workspace.CurrentCamera, Info2, Properties2)

local Debounce = false
local BeamTimeLine = game.ReplicatedStorage.BeamAbility.Timeline.Value

game:GetService("RunService").Stepped:Connect(function()
	
end)

UIS.InputBegan:Connect(function(input, event)
	if event then return end
	if input.KeyCode == Enum.KeyCode.E and Debounce == false then
		Debounce = true
		
		game.ReplicatedStorage.BeamAbility:FireServer(mouse.Hit.p, "clone")
		
		repeat wait()
			game.ReplicatedStorage.BeamAbility:FireServer(mouse.Hit.p, "followMouse")
		until Debounce == false
		
	end
end)

You need to set the debounce to false; Also in the server script the state variable is never changed to “Finished”; And I would suggest depending on the situation giving network ownership of the model that’s moving to the player.

What I’m trying to say is that my script works, kind of but it’s in-efficent.

Do the VFX in client, instead of server or it’ll have delay for the server to replicate to client, if you need it in the server, instead of 0.001 intervals for wait(), lerp the movement every wait(0.1) or any big number that looks best.

I just realized you already set the network owner of the model to the player, but you are still changing its position on the server.

1 Like

Position has to be changed via the server because it needs to repliacte for every other player in the server.

when you set the network owner of a part to a player it will replicate to other players
https://developer.roblox.com/en-us/api-reference/function/BasePart/SetNetworkOwner


and instead of Model.HandPart:SetNetworkOwner(plr)
do for _, v in pairs(Model:GetDescendants()) do if v:IsA("BasePart") then v:SetNetworkOwner(plr) end end

And use FireAllClients() to replicate effects to every client.

What would this do exactly lol?

So the effect appears for everyone, and instead of it running from the server, it runs from the clients directly. Server to client takes more time, than directly client to client

clients cant fire events to eachother, it must go from client to server to client. Changing the network ownership of a basepart means that the client handles the physics simulation and properties of the basepart, then it gets sent to the server where the server sends it to the other clients; even though it has to go through the server it still takes a lot of work off the server and makes it look smoother.

I mean to do it from client > server > clients.

Yes but there is nothing to fire the clients with, game data like the position of baseparts is handled internally through roblox and is faster, so when you set the network owner of a part to a client and the client moves that part, it will automatically replicate to the other clients

Oh, I think firing to all clients is more practical, though. Network ownership has delay too.

By the way, what is this for? I’m sure anchored parts is useless for physics to play with network ownership

Okay, I fixed it but there’s a slight issue, how to I make a certain distance for it to go?
I have this at the moment for distance wise.

game:GetService("RunService").Stepped:Connect(function()
	if Enabled == true then
		local range = math.min(50, (hrp.Position - pos).Magnitude)
		local direction = (hrp.Position - pos)
		
		plr.Character:WaitForChild("LeftHand"):WaitForChild("Beam").HandPart.CFrame = plr.Character.LeftHand.CFrame
		plr.Character:WaitForChild("LeftHand"):WaitForChild("Beam").HitPart.Position = mouse.Hit.Position	
		
		if (plr.Character:WaitForChild("LeftHand"):WaitForChild("Beam").HitPart.Position - hrp.Position).Magnitude < 25 then
			plr.Character:WaitForChild("LeftHand"):WaitForChild("Beam").HitPart.Position = plr.Character:WaitForChild("LeftHand"):WaitForChild("Beam").HitPart.Position - Vector3.new(5,0,5)
		end

	else
		return
	end
end)

@keith_220 @Winbloo

Wouldn’t this just be a loop of plr.Character:WaitForChild("LeftHand"):WaitForChild("Beam").HitPart.Position = mouse.Hit.Position and if (plr.Character:WaitForChild("LeftHand"):WaitForChild("Beam").HitPart.Position - hrp.Position).Magnitude < 25 then plr.Character:WaitForChild("LeftHand"):WaitForChild("Beam").HitPart.Position = plr.Character:WaitForChild("LeftHand"):WaitForChild("Beam").HitPart.Position - Vector3.new(5,0,5) end

And to subtract vector3 do:
.Position = .Position * Vector3.new(-5,0,-5)

Render.Stepped, is per frame. So it loops every frame.

I know, I meant it would just loop both of it if it’s less than 25, because the first position would loop regardless.

It can change the position twice in a single iteration.

So, what would be the fix, because I’m unsure.