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

How to target player part inside of a Model Script?

Asked by 7 years ago
Edited 7 years ago

I have a script that works fairly well for pathfinding using the pathfinding service, but I can only get it to target parts, I keep getting errors describing that UpperTorso is not part of the player. I haven't found any docs referring to the how to identify the R15 parts. Not sure if Upper Torso is right, I used it bc it was the CanCollide = true.

Also I am confused I think about how to call the player depending on if Im in localscripts or general ones. Actually, I don't think I've gotten the player into a script yet... :(

So my script is inside a model called "bot" which is a NPC model. I have my function for FindPath and several other associated functions. I call the function like this and it works fine (workspace.globe is just a part):

while true do
    wait(0.1)
    FindPath(workspace.globe.Position, 1, 1, 500)   
end     

Then I try this and nothing:

while true do
    wait(0.1)
local character = game.Workspace.Player
local player = game.Players:GetPlayerFromCharacter(character)

    FindPath(player.UpperTorso.Position, 1, 1, 500)
end

Thank you for any help!!!

Edit: Here is the entire script per request:

local pathfinding = game:GetService("PathfindingService")
local Bot = script.Parent
local Hum = Bot.Humanoid
local Tor = Bot.Torso
local Showed = {}

local function NumTab(T)
    local N = 0
    for i,v in pairs(T) do
    N = N + 1
end
    return N
end

local Bot = {}

FindPath = function(finish, PointM, MaxPoint, MaxDist)
    local MaxPoint = MaxPoint or 1
    local PointM = PointM or 1
    local MaxDist = MaxDist or 500
    local Am = 1
    local path = pathfinding:ComputeRawPathAsync(Tor.Position, finish, MaxDist)
local points = path:GetPointCoordinates()
for _, point in pairs(points) do
    if Am == PointM then
        Hum:MoveTo(point)
        repeat
            distance = (point - Tor.Position).magnitude
            wait()
        until distance < 3
    end
    if Am == MaxPoint then
        Am = 1
    else
        Am = Am + 1
    end
end
end

ShowPath = function(start, finish)
local path = pathfinding:ComputeRawPathAsync(start, finish, 500)
local points = path:GetPointCoordinates()
for _, point in pairs(points) do
    local part = Instance.new("Part", Workspace)
    part.FormFactor = Enum.FormFactor.Custom
    part.Size = Vector3.new(1,1,1)
    part.Position = point
    part.Anchored = true
    part.CanCollide = false
    table.insert(Showed, part)
end
end

ClearPaths = function()
for i,v in pairs(Showed) do
    v:Destroy()
    Showed[i] = nil
end
end

GetBackwardsCoordinates = function(Path)
local BackPath = {}
local points = Path:GetPointCoordinates()
for i = NumTab(points), 1, -1 do
    table.insert(BackPath, points[1])
end
return BackPath
end

GetBackwardsPath = function(Path)
local points = Path:GetPointCoordinates()
local finish = points[1]
local start = points[#points]
return pathfinding:ComputeRawPathAsync(start, finish, 500)
end

MovePlayer = function(Player, finish)
local Char = Player.Character
local Tor = Char.Torso
local Hum = Char.Humanoid
local path = pathfinding:ComputeRawPathAsync(Tor.Position, finish, 500)
local points = path:GetPointCoordinates()
local point = points[1]
if point then
    Hum:MoveTo(point)
    repeat
        distance = (point - Tor.Position).magnitude
        wait()
    until distance < 3
    Bot.MovePlayer(Player, finish)
end
end

while true do
wait(0.1)


    FindPath(workspace.globe.Position, 1, 1, 500)   
end
0
Please add your FindPath function, as i think the issue is with that. (You only send a StartPosition, not an End Position) RubenKan 3615 — 7y
0
local pathfinding = game:GetService("PathfindingService") local Bot = script.Parent local Hum = Bot.Humanoid local Tor = Bot.Torso local Showed = {} local function NumTab(T) local N = 0 for i,v in pairs(T) do N = N + 1 end return N end local Bot = {} FindPath = function(finish, PointM, MaxPoint, MaxDist) local MaxPoint = MaxPoint or 1 local PointM = PointM or 1 local MaxDist = MaxDist Eagle_RG 2 — 7y
0
Sorry, that looks crazy on my screen. Did I post it right?? Eagle_RG 2 — 7y
0
@RubenKan Sry, Im a noob here. I posted the entire script into answers. Eagle_RG 2 — 7y

Answer this question