Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
6

Character Controls into another Model w/ Humanoid?

Asked by
Ryukiyo 65
8 years ago

I apologize in advance for butchering my question, but what I'm pretty much trying to do is 'transfer', or replicate, default character controls into another model that has a humanoid (meaning 'W' will move the model forward depending on the direction the camera is facing, 'D' will move the model backwards, etc.)

There was a Wiki article explaining how to create and manipulate player movement that I found useful and helped provide a starting point for me. The article can be found here.

What I have so far works pretty well, but it's not what I want. Here's the code I've taken from the Wiki, and edited so it works alongside what I have at the moment;

--// Script provided by the ROBLOX Wiki

local userInputService = game:GetService("UserInputService")
local runService = game:GetService("RunService")
local player = game.Players.LocalPlayer
local moveVector = Vector3.new(0,0,0)

userInputService.InputBegan:connect(function(inputObject)

    local poke = workspace:FindFirstChild(player.Name.."'s Pokemon")

    if poke then
        if inputObject.KeyCode == Enum.KeyCode.A then
            moveVector = moveVector + Vector3.new(1,0,0)
        end
        if inputObject.KeyCode == Enum.KeyCode.D then
            moveVector = moveVector + Vector3.new(-1,0,0)
        end
        if inputObject.KeyCode == Enum.KeyCode.Space then
            poke.Humanoid.Jump = true
        end
    end
end)

userInputService.InputEnded:connect(function(inputObject)

    local poke = workspace:FindFirstChild(player.Name.."'s Pokemon")

    if poke then
        if inputObject.KeyCode == Enum.KeyCode.A then
            moveVector = moveVector + Vector3.new(-1,0,0)
        end
        if inputObject.KeyCode == Enum.KeyCode.D then
            moveVector = moveVector + Vector3.new(1,0,0)
        end
    end
end)

runService.RenderStepped:connect(function()

    local poke = workspace:FindFirstChild(player.Name.."'s Pokemon")

    if poke then
        poke.Humanoid:Move(moveVector)
    end
end)

Here's a GIF showing how the code works in-game so far; Link

As you can tell within the script and GIF, the controls don't function exactly like the default controls- which is where I need some assistance. Upon pressing A or D, the model will only move relative to the world space, not the model's position or the direction of the camera.

I've worked with Lua for a while now, but this is certainly one of the things that continue to puzzle me no matter how hard I try to attempt it.

What would I need to do in order to accomplish the task I'm trying to complete? Or is there a much easier solution around this problem?

I don't necessarily need a definite answer, but I'd appreciate any general tips that could lead me to the correct path. Thanks!

0
When I wanted to control a part all I did was player.Character = workspace.Part Perci1 4988 — 8y
0
@Perci1 I tried this, and it returns an error saying "bad cast" aquathorn321 858 — 8y
0
I wish it were as simple as that, but I need to keep the player's default character as well. If I try setting the character to another part, the player's character would be removed. Ryukiyo 65 — 8y
0
A little late, but why not just clone the player's character & set the CFrame and such and anchor it? Then you can just set the character to the part (or a clone of the part, if the original is important), and set it back to the character. Pyrondon 2089 — 8y

1 answer

Log in to vote
2
Answered by 8 years ago

Sorry for a late answer, but I'm mostly answering this for anyone else that needs help with this.

Perci1 was essentially correct about his solution, but he made a tiny mistake. Set the LocalPlayer's Character to the model of the target character. When you are finished controlling that character, set it back to the old model. Perhaps something like this:

local Player = game.Players.LocalPlayer
local CachedCharacter = Player.Character
local Pokemon = --[[whatever your pokemon is]]

function ControlPokemon()
    Player.Character = Pokemon
end
function ControlSelf()
    Player.Character = CachedCharacter
end

If the camera does not move to the Pokemon, you may have to set CurrentCamera's CameraSubject property to the Pokemon when you change, and vice versa with the regular character.

0
1. where do i put this script/localscript yuo_fool 0 — 4y
0
2. it didnt work when i was trying to control a mesh part is that a problem? yuo_fool 0 — 4y
Ad

Answer this question