I’m trying to make a puzzle game where the keys in your keyboard are switched out for other keys, so I had written some code for the switching keys part.
robloxapp-20230905-2130053.wmv (1.2 MB)
local RunService = game:GetService("RunService")
local Players = game:GetService("Players")
local UserInputService = game:GetService("UserInputService")
local player = Players.LocalPlayer
function switchKeyStartW(key)
UserInputService.InputBegan:Connect(function(input, gameProcessed)
if input.KeyCode == key then
if input.UserInputType == Enum.UserInputType.Keyboard then
print("A key is being pushed down! Key:", input.KeyCode)
RunService:BindToRenderStep("move", Enum.RenderPriority.Character.Value + 1, function()
if player.Character then
local humanoid = player.Character:FindFirstChild("Humanoid")
if humanoid then
humanoid:Move(Vector3.new(0, 0, -1), true)
end
end
end)
end
end
end)
end
function switchKeyStartD(key)
UserInputService.InputBegan:Connect(function(input, gameProcessed)
if input.KeyCode == key then
if input.UserInputType == Enum.UserInputType.Keyboard then
print("A key is being pushed down! Key:", input.KeyCode)
RunService:BindToRenderStep("move", Enum.RenderPriority.Character.Value + 1, function()
if player.Character then
local humanoid = player.Character:FindFirstChild("Humanoid")
if humanoid then
humanoid:Move(Vector3.new(1, 0, 0), true)
end
end
end)
end
end
end)
end
function switchKeyEnd(key)
UserInputService.InputEnded:Connect(function(input, gameProcessed)
if input.KeyCode == key then
if input.UserInputType == Enum.UserInputType.Keyboard then
print("A key is being pushed down! Key:", input.KeyCode)
RunService:BindToRenderStep("move", Enum.RenderPriority.Character.Value + 1, function()
if player.Character then
local humanoid = player.Character:FindFirstChild("Humanoid")
if humanoid then
humanoid:Move(Vector3.new(0, 0, 0), true)
end
end
end)
end
end
end)
end
UserInputService.InputBegan:Connect(switchKeyStartW(Enum.KeyCode.E))
UserInputService.InputEnded:Connect(switchKeyEnd(Enum.KeyCode.E))
UserInputService.InputBegan:Connect(switchKeyStartD(Enum.KeyCode.Q))
UserInputService.InputEnded:Connect(switchKeyEnd(Enum.KeyCode.Q))
[I did only E-W and D-Q]
However, the script happens to be quite long and it doesn’t even seem to work properly. I can’t move diagonally and my character often stops when i press the keys in quick succession. Any help regarding this would be much appreciated. (i will probably respond late)