#Need help with Vector2

1 messages · Page 1 of 1 (latest)

bitter fjord
#

up and down work but left and right do not, ive asked 50 different AI's cos i aint a very good scripter and none of them solved my problem

-- Allow T (up), F (left), G (down), H (right)
local keyToDirection = {
    [Enum.KeyCode.T] = Vector2.new(0, -1), -- Up
    [Enum.KeyCode.F] = Vector2.new(-1, 0), -- Left
    [Enum.KeyCode.G] = Vector2.new(0, 1),  -- Down
    [Enum.KeyCode.H] = Vector2.new(1, 0),  -- Right
}
tacit thistle
#

What's the context twin

arctic light
#

Like why don't they work?

tacit thistle
#

No I meant how r u interpolating a vector2 into direction what's the rest of the code

bitter fjord
# tacit thistle No I meant how r u interpolating a vector2 into direction what's the rest of the...
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local moveEvent = ReplicatedStorage:WaitForChild("MoveAttachmentOnWall")

-- Allow T (up), F (left), G (down), H (right)
local keyToDirection = {
    [Enum.KeyCode.T] = Vector2.new(0, -1), -- Up
    [Enum.KeyCode.F] = Vector2.new(-1, 0), -- Left
    [Enum.KeyCode.G] = Vector2.new(0, 1),  -- Down
    [Enum.KeyCode.H] = Vector2.new(1, 0),  -- Right
}

local heldKeys = {}

task.spawn(function()
    while true do
        local moveVec = Vector2.new(0, 0)
        for key, held in heldKeys do
            if held and keyToDirection[key] then
                moveVec = moveVec + keyToDirection[key]
            end
        end
        if moveVec.Magnitude > 0 then
            print("Client sending moveVec:", moveVec)
            moveEvent:FireServer(moveVec)
        end
        task.wait(0.02)
    end
end)

UserInputService.InputBegan:Connect(function(input, gameProcessed)
    if gameProcessed then return end
    if input.UserInputType == Enum.UserInputType.Keyboard then
        local key = input.KeyCode
        print("Key pressed:", key.Name)
        if keyToDirection[key] then
            heldKeys[key] = true
        end
    end
end)

UserInputService.InputEnded:Connect(function(input, gameProcessed)
    if input.UserInputType == Enum.UserInputType.Keyboard then
        local key = input.KeyCode
        print("Key released:", key.Name)
        if heldKeys[key] then
            heldKeys[key] = false
        end
    end
end)```
arctic light
#

Wait I'm not the op here if you thought that

bitter fjord
#

you can see all keys are tracked, but only the set keybinds "TFGH" are given the distance they moved, T and G (up and down) change whilst F and H do not