Turning this script into server

How would I go about converting this local script inside starter character scripts into a server script so its less jittery and laggy?

script:


local player = game.Players.LocalPlayer
local mouse = player:GetMouse()

local char = player.Character or player.CharacterAdded:Wait()
local hum = char:WaitForChild("Humanoid")

local decreaseSpeed = 0.1
local increaseSpeed = 0.1
local minWalkSpeed = 20
local maxWalkSpeed = 50

local decreasing = false
local increasing = false

mouse.Button1Down:Connect(function()
	if hum and hum.WalkSpeed < maxWalkSpeed then
		increasing = true
		decreasing = false
	end
end)

mouse.Button1Up:Connect(function()
	if hum and hum.WalkSpeed > minWalkSpeed then
		decreasing = true
		increasing = false
	end
end)

mouse.Button2Down:Connect(function()
	increasing = false
	decreasing = false
end)

game:GetService("RunService").RenderStepped:Connect(function()
	if hum and increasing then
		hum.WalkSpeed = math.min(hum.WalkSpeed + increaseSpeed, maxWalkSpeed)
	elseif hum and decreasing then
		hum.WalkSpeed = math.max(hum.WalkSpeed - decreaseSpeed, minWalkSpeed)
	end
end)

mouse.Move:Connect(function()
	local mousehit = mouse.Hit.Position
	hum:MoveTo(mousehit)
end)

Start researching Remote Events. they transfer input data from the client to the server.

as @keremMCT said you should use events.
make a event with whatever name you please, here I used “WalkSpeedEvent”
-place it into replicatedstorage

and you can use these scripts as examples

local script :+1:

local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local WalkSpeedEvent = ReplicatedStorage:WaitForChild("WalkSpeedEvent")

-- ... rest of your code ...

mouse.Button1Down:Connect(function()
	if hum and hum.WalkSpeed < maxWalkSpeed then
		WalkSpeedEvent:FireServer("increase")
	end
end)

mouse.Button1Up:Connect(function()
	if hum and hum.WalkSpeed > minWalkSpeed then
		WalkSpeedEvent:FireServer("decrease")
	end
end)

-- ... rest of your code ...

Server script :

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")
local WalkSpeedEvent = ReplicatedStorage:WaitForChild("WalkSpeedEvent")

local function changeWalkSpeed(player, action)
	local char = player.Character
	local hum = char and char:FindFirstChild("Humanoid")
	
	if hum then
		if action == "increase" and hum.WalkSpeed < maxWalkSpeed then
			hum.WalkSpeed = math.min(hum.WalkSpeed + increaseSpeed, maxWalkSpeed)
		elseif action == "decrease" and hum.WalkSpeed > minWalkSpeed then
			hum.WalkSpeed = math.max(hum.WalkSpeed - decreaseSpeed, minWalkSpeed)
		end
	end
end

WalkSpeedEvent.OnServerEvent:Connect(changeWalkSpeed)

also let me know If I messed up in the code.

1 Like

Alright so I did this but my character still sometimes lags back…

example: https://gyazo.com/34c6ffff29133c133ff186c111e16c40.mp4

local script:

 local player = game.Players.LocalPlayer
local mouse = player:GetMouse()

local increaseRemote = game.ReplicatedStorage:WaitForChild("increase")
local decreaseRemote = game.ReplicatedStorage:WaitForChild("decrease")
local moveRemote = game.ReplicatedStorage:WaitForChild("move")

local char = player.Character or player.CharacterAdded:Wait()
local hum = char:WaitForChild("Humanoid")

local decreaseSpeed = 0.1
local increaseSpeed = 0.1
local minWalkSpeed = 20
local maxWalkSpeed = 50

local decreasing = false
local increasing = false

mouse.Button1Down:Connect(function()
	if hum and hum.WalkSpeed < maxWalkSpeed then
		increasing = true
		decreasing = false
	end
end)

mouse.Button1Up:Connect(function()
	if hum and hum.WalkSpeed > minWalkSpeed then
		decreasing = true
		increasing = false
	end
end)

mouse.Button2Down:Connect(function()
	increasing = false
	decreasing = false
end)

game:GetService("RunService").RenderStepped:Connect(function()
	if hum and increasing then
		increaseRemote:FireServer(hum,increaseSpeed,maxWalkSpeed)
	elseif hum and decreasing then
		decreaseRemote:FireServer(hum,decreaseSpeed,minWalkSpeed)
	end
end)

mouse.Move:Connect(function()
	local mousehit = mouse.Hit.Position
	moveRemote:FireServer(hum,mousehit)
end)

serverscript:

local increaseRemote = game.ReplicatedStorage.increase
local decreaseRemote = game.ReplicatedStorage.decrease
local moveRemote = game.ReplicatedStorage:WaitForChild("move")

increaseRemote.OnServerEvent:Connect(function(player,hum,increaseSpeed,maxWalkSpeed)
	hum.WalkSpeed = math.min(hum.WalkSpeed + increaseSpeed, maxWalkSpeed)
end)

decreaseRemote.OnServerEvent:Connect(function(player,hum,decreaseSpeed,minWalkSpeed)
	hum.WalkSpeed = math.max(hum.WalkSpeed - decreaseSpeed, minWalkSpeed)
end)

moveRemote.OnServerEvent:Connect(function(player,hum,mousehit)
	hum:MoveTo(mousehit)
end)

Ah, I see this. You could implement a rate limiting system. This system would limit the number of times the moveRemote:FireServer function can be called in a certain period of time.

Also the client could probably manipulate the increasing of walkspeed so that should be validated or moved to the server.

im having problems with the jittering of the rocket

Change your .RenderStepped to something that doesn’t run every frame.

Code:

local player = game.Players.LocalPlayer
local mouse = player:GetMouse()

local increaseRemote = game.ReplicatedStorage:WaitForChild("increase")
local decreaseRemote = game.ReplicatedStorage:WaitForChild("decrease")
local moveRemote = game.ReplicatedStorage:WaitForChild("move")

local char = player.Character or player.CharacterAdded:Wait()
local hum = char:WaitForChild("Humanoid")

local decreaseSpeed = 0.1
local increaseSpeed = 0.1
local minWalkSpeed = 20
local maxWalkSpeed = 50

local decreasing = false
local increasing = false

mouse.Button1Down:Connect(function()
	if hum and hum.WalkSpeed < maxWalkSpeed then
		increasing = true
		decreasing = false
	end
end)

mouse.Button1Up:Connect(function()
	if hum and hum.WalkSpeed > minWalkSpeed then
		decreasing = true
		increasing = false
	end
end)

mouse.Button2Down:Connect(function()
	increasing = false
	decreasing = false
end)

mouse.Move:Connect(function()
	local mousehit = mouse.Hit.Position
	moveRemote:FireServer(hum,mousehit)
end)

while task.wait(0.1) do
	if hum and increasing then
		increaseRemote:FireServer(hum,increaseSpeed,maxWalkSpeed)
	elseif hum and decreasing then
		decreaseRemote:FireServer(hum,decreaseSpeed,minWalkSpeed)
	end
end

If this works, give @grandisy the solution because he’s the one who did the hard part.

1 Like

this could possibly be the solution

im still having jittering unfortunately :frowning:

fireserver() possibly causes network latency. you could try to debug this. The movement could also be controlled by the client so that you don’t need server remotevents.
Also maybe there could be something going on with networkownership, I am not 100% sure about your situation, I am not very experienced unfortunately.

You can try to think of debug solutions and come back to this.

Ill try to peck at it until I can find a solution i was hoping someone else had a solution though (didnt mean to reply to this)

Hey I figured it out!

local player = game.Players.LocalPlayer
local mouse = player:GetMouse()

local increaseRemote = game.ReplicatedStorage:WaitForChild("increase")
local decreaseRemote = game.ReplicatedStorage:WaitForChild("decrease")
local moveRemote = game.ReplicatedStorage:WaitForChild("move")

local char = player.Character or player.CharacterAdded:Wait()
local hum = char:WaitForChild("Humanoid")

local decreaseSpeed = 0.1
local increaseSpeed = 0.1
local minWalkSpeed = 20
local maxWalkSpeed = 50

local decreasing = false
local increasing = false

mouse.Button1Down:Connect(function()
	if hum and hum.WalkSpeed < maxWalkSpeed then
		increasing = true
		decreasing = false
	end
end)

mouse.Button1Up:Connect(function()
	if hum and hum.WalkSpeed > minWalkSpeed then
		decreasing = true
		increasing = false
	end
end)

mouse.Button2Down:Connect(function()
	increasing = false
	decreasing = false
end)


	


game:GetService("RunService").RenderStepped:Connect(function()
	wait()
	
	local mousehit = mouse.Hit.Position
	moveRemote:FireServer(hum,mousehit)
	
	if hum and increasing then
		wait()
		hum.WalkSpeed = math.min(hum.WalkSpeed + increaseSpeed, maxWalkSpeed)
	elseif hum and decreasing then
		wait()
		hum.WalkSpeed = math.max(hum.WalkSpeed - decreaseSpeed, minWalkSpeed)
	end
end)

I decided to instead of mouse.move to do the moving inside the renderstepped function which seems to be working!

1 Like

I have no idea why mouse.move made it so jittery though, i hope someone gives me an explanation for me.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.