I just want to know if having many loops such as while loops, or using the RunService Heartbeat or RenderStepped can cause lag. I’m asking this since a swimming system I’m making uses two heartbeat functions from the RunService, and they always run, and then a while loop comes in when certain conditions are met. These are all in one LocalScript. Would several players each having this script get lag, or something like that?
local plr = game.Players.LocalPlayer
local char = plr.Character or plr.CharacterAdded:Wait()
local OceanHeight = 395.74
local Swimming = false
local HRP = char:WaitForChild("HumanoidRootPart")
if workspace.CurrentCamera:FindFirstChild("BlurEffect") then
workspace.CurrentCamera.BlurEffect:Destroy()
end
local Blur = Instance.new("BlurEffect")
Blur.Parent = workspace.CurrentCamera
Blur.Enabled = false
local Underwater = false
local SpacePressed = false
game:GetService("RunService").Heartbeat:Connect(function()
wait(1)
if HRP.Position.Y > OceanHeight then
Swimming = false
elseif HRP.Position.Y <= OceanHeight then
Swimming = true
end
end)
game:GetService("RunService").Heartbeat:Connect(function()
if Swimming == true then
Blur.Enabled = true
script.Parent.Frame.Visible = true
char.Humanoid.JumpPower = 0
if HRP.Position.Y < 395 then
Underwater = true
Float = game:GetService("UserInputService").InputBegan:Connect(function(input,gameProcessed)
if gameProcessed then return end
if input.KeyCode == Enum.KeyCode.Space then
if Swimming == false then return end
if HRP:FindFirstChild("UnderwaterForce") then
HRP:FindFirstChild("UnderwaterForce"):Destroy()
end
local UF = Instance.new("BodyPosition")
UF.Parent = HRP
UF.P = 50000
UF.MaxForce = Vector3.new(0,math.huge,0)
UF.Name = "UnderwaterForce"
print("Space pressed")
SpacePressed = true
while Underwater == true do
if HRP.Position.Y >= OceanHeight or SpacePressed == false or Underwater == false then
UF:Destroy()
break
end
if SpacePressed then
UF.Position = Vector3.new(0,HRP.Position.Y + 6,0)
end
wait()
print("Space pressed, underwater")
end
end
end)
if Swimming == false then
Float:Disconnect()
end
FloatDestroy = game:GetService("UserInputService").InputEnded:Connect(function(input,gameProcessed)
if gameProcessed then return end
if input.KeyCode == Enum.KeyCode.Space then
SpacePressed = false
if HRP:FindFirstChild("UnderwaterForce") ~= nil then
HRP:FindFirstChild("UnderwaterForce"):Destroy()
end
end
end)
if Swimming == false then
FloatDestroy:Disconnect()
end
elseif HRP.Position.Y > 395 and HRP.Position.Y <= OceanHeight then
Underwater = false
if HRP:FindFirstChild("SwimForce") then
HRP:FindFirstChild("SwimForce"):Destroy()
elseif HRP:FindFirstChild("SwimForce") == nil then
local SF = Instance.new("BodyPosition")
SF.Parent = HRP
SF.Name = "SwimForce"
SF.Position = Vector3.new(HRP.Position.X,OceanHeight,HRP.Position.Z)
end
end
elseif Swimming == false then
Blur.Enabled = false
script.Parent.Frame.Visible = false
char.Humanoid.JumpPower = 50
if HRP:FindFirstChild("SwimForce") then
HRP:FindFirstChild("SwimForce"):Destroy()
end
end
end)
--while Swimming == true do
--print(Swimming)
--if HRP.Position.Y < 395 then
--elseif HRP.Position.Y > 395 and HRP.Position.Y <= OceanHeight then
--end
--wait(1)
--end