Hi all. I want to create a simple client and serverside helicopter using gyros and bodyvelocities. Unfortunately, I’m not the most experienced coder and have encountered some issues with my scripting. There are no errors in the logs when I run the game. Here is my client and serverside code.
Client:
local RS = game:GetService("ReplicatedStorage")
local UIP = game:GetService("UserInputService")
local pilotseatedinfo = RS:WaitForChild("currentplayerpilot")
local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local controllinghelicopter = false
local controlledhelicopter = nil
local controllinghelicopterseat = nil
pilotseatedinfo.OnClientEvent:Connect(function(currentplayerpilot, seated, helicopter)
if seated then
controllinghelicopter = true
controlledhelicopter = helicopter
else
controllinghelicopter = false
controlledhelicopter = nil
end
end)
local function onInputBegan(input, gameProcessedEvent)
if not gameProcessedEvent and controlledhelicopter then
if input.KeyCode == Enum.KeyCode.W then
print("W key pressed")
controlledhelicopter:FindFirstChild("engine"):FindFirstChildOfClass("BodyVelocity").Velocity = Vector3.new(controlledhelicopter:FindFirstChild("engine"):FindFirstChildOfClass("BodyVelocity").Velocity.X + 1, controlledhelicopter:FindFirstChild("engine"):FindFirstChildOfClass("BodyVelocity").Velocity.Y, controlledhelicopter:FindFirstChild("engine"):FindFirstChildOfClass("BodyVelocity").Velocity.Z)
elseif input.KeyCode == Enum.KeyCode.A then
print("A key pressed")
elseif input.KeyCode == Enum.KeyCode.S then
print("S key pressed")
elseif input.KeyCode == Enum.KeyCode.D then
print("D key pressed")
elseif input.KeyCode == Enum.KeyCode.E then
print("E key pressed")
elseif input.KeyCode == Enum.KeyCode.Q then
print("Q key pressed")
end
end
end
local function onInputEnded(input)
if controlledhelicopter then
if input.KeyCode == Enum.KeyCode.W then
print("W key released")
elseif input.KeyCode == Enum.KeyCode.A then
print("A key released")
elseif input.KeyCode == Enum.KeyCode.S then
print("S key released")
elseif input.KeyCode == Enum.KeyCode.D then
print("D key released")
elseif input.KeyCode == Enum.KeyCode.E then
print("E key released")
elseif input.KeyCode == Enum.KeyCode.Q then
print("Q key released")
end
end
end
UIP.InputBegan:Connect(onInputBegan)
UIP.InputEnded:Connect(onInputEnded)
Server
local UIP = game:GetService("UserInputService")
local RS = game.ReplicatedStorage
local players = game:GetService("Players")
local seat = script.Parent
local helicopter = seat.Parent
local pilotseatedinfo = RS:WaitForChild("currentplayerpilot")
local currentplayerpilot = nil
local seated = false
seat:GetPropertyChangedSignal("Occupant"):Connect(function()
local humanoid = seat.Occupant
if humanoid then
local character = humanoid.Parent
local player = players:GetPlayerFromCharacter(character)
if player then
seated = true
print(player.Name .. " has sat down")
currentplayerpilot = player
pilotseatedinfo:FireClient(currentplayerpilot,seated,seat)
return
end
end
if currentplayerpilot then
seated = false
print(currentplayerpilot.Name .. " has got up")
pilotseatedinfo:FireClient(currentplayerpilot, seated,helicopter)
currentplayerpilot = nil
end
end)
(helicopter client is in startercharacterscripts if that helps)