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)
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
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)
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)
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.
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.
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.
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!