Вот простой пример скрипта для движения персонажа с помощью клавиши "fly" (полет) в Roblox на языке Lua. Этот скрипт можно добавить в LocalScript, который должен быть помещен в StarterPlayerScripts, чтобы управлять полетом игрока.
lua
-- Скрипт полёта для Roblox
local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")
local rootPart = character:WaitForChild("HumanoidRootPart")
local flying = false
local speed = 50 -- Скорость полёта
local UserInputService = game:GetService("UserInputService")
local RunService = game:GetService("RunService")
local flyBodyVelocity
local function startFlying()
if flying then return end
flying = true
flyBodyVelocity = Instance.new("BodyVelocity")
flyBodyVelocity.Velocity = Vector3.new(0,0,0)
flyBodyVelocity.MaxForce = Vector3.new(1e5, 1e5, 1e5)
flyBodyVelocity.Parent = rootPart
humanoid.PlatformStand = true -- отключаем физику прыжков/движения персонажа
end
local function stopFlying()
if not flying then return end
flying = false
if flyBodyVelocity then
flyBodyVelocity:Destroy()
flyBodyVelocity = nil
end
humanoid.PlatformStand = false
end
local moveDirection = Vector3.new(0,0,0)
UserInputService.InputBegan:Connect(function(input, gameProcessed)
if gameProcessed then return end
if input.KeyCode == Enum.KeyCode.F then -- Нажатие F включает/выключает полёт
if flying then
stopFlying()
else
startFlying()
end
end
end)
UserInputService.InputEnded:Connect(function(input, gameProcessed)
if gameProcessed then return end
-- Обнуляем направления при отпускании клавиш WASD
if input.KeyCode == Enum.KeyCode.W or input.KeyCode == Enum.KeyCode.S or
input.KeyCode == Enum.KeyCode.A or input.KeyCode == Enum.KeyCode.D or
input.KeyCode == Enum.KeyCode.Space or input.KeyCode == Enum.KeyCode.LeftShift then
moveDirection = Vector3.new(0,0,0)
end
end)
-- Обработка нажатия для движения
local keysDown = {}
UserInputService.InputBegan:Connect(function(input, gameProcessed)
if gameProcessed then return end
if flying then
if input.KeyCode == Enum.KeyCode.W then
keysDown["W"] = true
elseif input.KeyCode == Enum.KeyCode.S then
keysDown["S"] = true
elseif input.KeyCode == Enum.KeyCode.A then
keysDown["A"] = true
elseif input.KeyCode == Enum.KeyCode.D then
keysDown["D"] = true
elseif input.KeyCode == Enum.KeyCode.Space then
keysDown["Space"] = true
elseif input.KeyCode == Enum.KeyCode.LeftShift then
keysDown["Shift"] = true
end
end
end)
UserInputService.InputEnded:Connect(function(input, gameProcessed)
if gameProcessed then return end
if flying then
if input.KeyCode == Enum.KeyCode.W then
keysDown["W"] = false
elseif input.KeyCode == Enum.KeyCode.S then
keysDown["S"] = false
elseif input.KeyCode == Enum.KeyCode.A then
keysDown["A"] = false
elseif input.KeyCode == Enum.KeyCode.D then
keysDown["D"] = false
elseif input.KeyCode == Enum.KeyCode.Space then
keysDown["Space"] = false
elseif input.KeyCode == Enum.KeyCode.LeftShift then
keysDown["Shift"] = false
end
end
end)
RunService.RenderStepped:Connect(function()
if flying and flyBodyVelocity then
local move = Vector3.new(0,0,0)
local camera = workspace.CurrentCamera
local lookVector = camera.CFrame.LookVector
local rightVector = camera.CFrame.RightVector
if keysDown["W"] then
move = move + lookVector
end
if keysDown["S"] then
move = move - lookVector
end
if keysDown["A"] then
move = move - rightVector
end
if keysDown["D"] then
move = move + rightVector
end
if keysDown["Space"] then
move = move + Vector3.new(0,1,0)
end
if keysDown["Shift"] then
move = move - Vector3.new(0,1,0)
end
move = move.Unit
if move ~= move then -- Проверка на NaN
move = Vector3.new(0,0,0)
end
flyBodyVelocity.Velocity = move * speed
end
end)