Help with UserInputService

So I have a cannon that was coded to use throttle and steer (Properties of a VehicleSeat) to rotate and turn the platform of the gun, but I want to make it so you press a button on a keyboard to move instead of using WASD. But, I can’t seem to figure out how to do that without an issue.

So far I tried to use events to connect a UserInputService function. Here’s my code:

Server

script.Key.OnServerEvent:Connect(function(key)
			if key == "Left" then
				if play == false then
					play = true
					sp.Platform.Turn:Play()
				end
				sp.Platform.Weld.C0 = 	sp.Platform.Weld.C0 * CFrame.fromEulerAnglesXYZ(0,math.rad(-set.PlatformRotate),0)
			elseif key == "Right" then
				if play == false then
					play = true
					sp.Platform.Turn:Play()
				end
				sp.Platform.Weld.C0 = 	sp.Platform.Weld.C0 * CFrame.fromEulerAnglesXYZ(0,math.rad(set.PlatformRotate),0)
			elseif key == "Up" then
				if rot + set.CannonRotate <= set.CRMaxNeg then
					sp.Rotor.Weld.C0 = sp.Rotor.Weld.C0 * CFrame.fromEulerAnglesXYZ(0,0,math.rad(set.CannonRotate))
					rot = rot + set.CannonRotate
					sp.Platform.Turn:Play()
				end
			elseif key == "Down" then
				sp.Platform.Turn:Play()
				if rot -set.CannonRotate >= -set.CRMaxPos then
					sp.Rotor.Weld.C0 = sp.Rotor.Weld.C0 * CFrame.fromEulerAnglesXYZ(0,0,math.rad(-set.CannonRotate))
					rot = rot - set.CannonRotate
					sp.Platform.Turn:Play()
				end
			end
		end)
		script.KeyOut.OnServerEvent:Connect(function()
			play = false
			sp.Platform.Turn:Stop()
		end)

Client

UIS.InputBegan:Connect(function(input,process)
	if input.UserInputType == Enum.UserInputType.Keyboard then
		if input.KeyCode == Enum.KeyCode.Left and process == false then
			local key = "Left"
			keyevent:FireServer(key)
		end
	end
	if input.UserInputType == Enum.UserInputType.Keyboard then
		if input.KeyCode == Enum.KeyCode.Right and process == false then
			local key = "Right"
			keyevent:FireServer(key)
		end
	end
	if input.UserInputType == Enum.UserInputType.Keyboard then
		if input.KeyCode == Enum.KeyCode.Up and process == false then
			local key = "Up"
			keyevent:FireServer(key)
		end
	end
	if input.UserInputType == Enum.UserInputType.Keyboard then
		if input.KeyCode == Enum.KeyCode.Down and process == false then
			local key = "Down"
			keyevent:FireServer(key)
		end
	end
end)

UIS.InputEnded:Connect(function(input,process)
	if input.UserInputType == Enum.UserInputType.Keyboard then
		if input.KeyCode == Enum.KeyCode.Left and process == false then
			keyeventstop:FireServer()
		end
	end
	if input.UserInputType == Enum.UserInputType.Keyboard then
		if input.KeyCode == Enum.KeyCode.Right and process == false then
			keyeventstop:FireServer()
		end
	end
end)

Have I been doing this right? Are there any alternatives? Any help is appreciated.

1 Like

It looks like you’re only detecting the arrow key input, from what I see from this script?

You could try detecting if the Player has hit the key using W or the Up Key maybe :thinking:

Also

OnServerEvent has the Player instance who fired it at the first parameter, so change that to

script.Key.OnServerEvent:Connect(function(Player, key)
2 Likes