Getting the character with movement keys

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    Based on my position i wan’t to get the character by pressing the movement keys

  2. What is the issue? Include screenshots / videos if possible!
    immagine_2023-01-10_131200675
    immagine_2023-01-10_131647322

  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I have no clue how to achieve this tbh
    I thought about a big cone raycast whenever you press a you get a left cone raycast if you press s+a you get it left and down based on your position but i don’t know if it’s a good idea

1 Like

You can achive this by getting every input the player has pressesd, then you could search the workspace for each char and calculate the distance between

I’m assuming the player doesn’t move. As for the rest here’s how I would go around doing this.

First of all I’d give those characters a name of some sort - if a character is meant to be for the W key then name it W.

After naming our fellow character W you can go ahead and check what key the player presses, using UserInputService.
It would look something like this:

local userInput = game:GetService('UserInputService') -- UserInputService to detect key presses.

local characterW = game.Workspace.W -- The character/NPC you want when pressing W.

userInput.InputBegan:Connect(function(input,gp) -- Fires when a key is pressed.
    if not gp then -- If the player isn't typing then it fires the code below.
        if input.KeyCode == Enum.KeyCode.W then
            print(characterW.Torso.Position) -- Prints the characterW's torso's position as an example
        end
    end
end)

Disclaimer - I’m writing all of this out of complete memory so if mistakes are done please say so.

1 Like

Those dummies were just an example of what i wanted, in the game they are players moving around and stuff.

Then just make a raycast (on left/right/front/back) and update its position every UserInputService.InputBegan as well as checking if there are any players colliding.

You can check if a player/NPC is colliding by detecting if a model is touching the raycast and afterwards detecting if there’s a Humanoid in the model.

Also if you still can’t figure it out yourself you don’t need to check on all left/right/front/back raycasts at the same time just use keys for them - on key A pressed check if the left raycast is colliding with anything.