hi, i want to have the player to control an npc, like if the player presses the keys on the keyboard not only he, but the npc will be controlled by it. let me know if i’ve been clear, thanks!
hmmm…
set the walkspeed of the player’s Humanoid to 0
make the NPC do :MoveTo()
with the .WalkToPoint
of the humanoid
i haven’t tested this but according to the docs it says this:
that and jump inputs have to be handled separately
omg i have no idea on how to do that
it’s actually… kind of simple? if you’re a beginner that’s ok but it’s very simple to just do this:
-- assume you know the player character already and the player humanoid
playerHumanoid.WalkSpeed = 0
-- idk what to even do insert other code here that does the MoveTo stuff
This is a bit advanced, since there’s a ton of niche things that you have to explore for yourself to get the desire you want.
If you didn’t know, Players will replicate their scripts into Players.LocalPlayer.PlayerScripts
, and in this Roblox will automatically put its own scripts and modules. These Roblox scripts are used to control a player, and in your context, it’s a perfect use to replicate movement to the NPC. You can dynamically look at these scripts if you run the game and look into your Player instance, since they do not exist before running the game.
Inside the PlayerScripts will be a module called PlayerModule
, which lets the game know how the player should move when a certain action is performed (are they in a vehicle and trying to move?). This module doesn’t have a lot, but it requires a child module called ControlModule
. This is the main thing that controls how a player will be moving.
You can stop Roblox from inserting the script by having a script of the same name under StarterPlayer.StarterPlayerScripts
. Here is a showcase of the primary variables you could be using:
local RunService = game:GetService("RunService")
local PlayerModule = require(script.Parent:WaitForChild("PlayerModule"))
local PlayerController = PlayerModule.controls
RunService.RenderStepped:Connect(function()
local CurrentController = PlayerController.activeController
-- activeController may be nil
if not CurrentController then return end
local WalkingDirection = CurrentController:GetMoveVector()
print(WalkingDirection)
end)
You can take a look inside PlayerModule.ControlModule.Keyboard and other of the modules located here to get a better understanding. I don’t know this inside and out, so I’m not sure what you can do for your specific problem, but if you look around for yourself I’m sure you can solve it.
its the next part i don’t know how to do
I would recommend reading those scripts one by one. It’s incredibly hard to read code that has no comments, even for experienced coders, so if you want to get better at coding then you should add your own comments so you can understand what certain functions do.
If you haven’t started with coding, I would stay away from this because of how difficult it is to understand this kind of code without comments.
Alternatively, if you dont want to do that, an incredibly cheap way you can replicate movement is just teleporting the NPC to the player and offsetting it by some amount. There’s better ways to do this, but it’s one way
local PlayerService = game:GetService("Players")
local RunService = game:GetService("RunService")
local GameWorkspace = game:GetService("Workspace")
-- errors if you put this in a server script
if RunService:IsServer() then
error("This code was specifically made in mind for only one player, so don't have it as a server script!")
end
local LocalPlayer = PlayerService.LocalPlayer
local NPCModel = GameWorkspace:WaitForChild("NPC")
RunService.RenderStepped:Connect(function()
local PlayerCharacter = LocalPlayer.Character
if not PlayerCharacter then return end
local PlayerOrigin = PlayerCharacter:GetPivot().Position
local PivotOffset = Vector3.new(0, 0, 20)
NPCModel:PivotTo(CFrame.new(PlayerOrigin + PivotOffset))
end)