Hello Devs,
I’ve recently created a working robot arm and was wondering if there is a more efficient way of writing my code. I feel like there is a much better way of doing this and I’m not quite sure how. I’m specifically looking for a better way to handle the inputs from the player.
Robot arm:
Local script
local UserInputService = game:GetService("UserInputService")
local remote = game.ReplicatedStorage:WaitForChild("RemoteEvent")
local player = game:GetService("Players").LocalPlayer
player.CharacterAdded:Connect(function(character)
remote:FireServer("Startup")
end)
UserInputService.InputBegan:Connect(function(input, typing)
if input.KeyCode == Enum.KeyCode.E and not typing then
remote:FireServer("Right")
end
if input.KeyCode == Enum.KeyCode.Q and not typing then
remote:FireServer("Left")
end
if input.KeyCode == Enum.KeyCode.R and not typing then
remote:FireServer("Up")
end
if input.KeyCode == Enum.KeyCode.F and not typing then
remote:FireServer("Down")
end
if input.KeyCode == Enum.KeyCode.Z and not typing then
remote:FireServer("Forward")
end
if input.KeyCode == Enum.KeyCode.C and not typing then
remote:FireServer("Backward")
end
if input.KeyCode == Enum.KeyCode.T and not typing then
remote:FireServer("PickUp")
end
end)
Server Script
local remote = game.ReplicatedStorage.RemoteEvent
remote.OnServerEvent:Connect(function(plr, message, action)
if message == "Startup" then
local Character = plr.Character
local Arm = game.Workspace.Arm:Clone()
Arm.Parent = Character.Torso
Character.HumanoidRootPart.Anchored = true
wait(0.1)
Arm.PrimaryPart.CFrame = Character.Torso.CFrame + Vector3.new(0, 0, 1)
Arm.PointTo.CFrame = Arm.PrimaryPart.CFrame + Vector3.new(0, 5, 0)
Arm.PrimaryPart.WeldConstraint.Part1 = Character.Torso
local weld = Instance.new("WeldConstraint")
weld.Parent = Arm.PrimaryPart
weld.Part0 = Arm.PrimaryPart
weld.Part1 = Arm.PointTo
Arm.PointTo.Anchored = false
wait(3)
Character.HumanoidRootPart.Anchored = false
end
local Character = plr.Character
local Arm = Character.Torso.Arm
if message == "Right" then
local lookvec = Arm.PointTo.CFrame.RightVector * 1
Arm.PointTo.Position = Arm.PointTo.Position + lookvec
end
if message == "Left" then
local lookvec = Arm.PointTo.CFrame.RightVector * -1
Arm.PointTo.Position = Arm.PointTo.Position + lookvec
end
if message == "Up" then
local lookvec = Arm.PointTo.CFrame.UpVector * 1
Arm.PointTo.Position = Arm.PointTo.Position + lookvec
end
if message == "Down" then
local lookvec = Arm.PointTo.CFrame.UpVector * -1
Arm.PointTo.Position = Arm.PointTo.Position + lookvec
end
if message == "Forward" then
local lookvec = Arm.PointTo.CFrame.LookVector * 1
Arm.PointTo.Position = Arm.PointTo.Position + lookvec
end
if message == "Backward" then
local lookvec = Arm.PointTo.CFrame.LookVector * -1
Arm.PointTo.Position = Arm.PointTo.Position + lookvec
end
end)
Please could someone help me, or at least point me in the right direction.