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

attempt to index upvalue 'targetTorso' (a nil value), how could I fix this [?]

Asked by 5 years ago
Edited 5 years ago

I thank Kingdom for providing a great example.

Objective:

My Objective is pretty simple, it's to create an NPC Respawn script that simply respawns the NPC to where it was from (place in studio) when it dies or falls out of the world. I'm trying to make scripts not laggy and avoid loops as much as possible.

Set-up:

in workspace I have a Folder called Bandits which holds all the NPC's. in ServerStorage I have a Folder called NPCBackup Which holds a new Bandit ready to be used. in ServerScriptService I have a script that manages the NPC's. in ReplicatedStorage I have a Folder called ModuleScripts That holds ModuleScripts.

Problem:

An error appears during testing but I'm not sure how to deal with it. When I try to make add an if statement, it just stops the entire system completely.

Script:

local RunService = game:GetService("RunService")
local Settings = require(game:GetService("ReplicatedStorage").ModuleScripts.SETTINGS)
local Respawn_Time = Settings.BANDIT.RESPAWN_TIME
local plrServ = game:GetService('Players')
local NPCBackup = game:GetService('ServerStorage').NPCBackup.Bandit

local function getClosestPlayer(myPos)
    local pos, tor = Settings.BANDIT.RANGE
    for _, plr in pairs(plrServ:GetPlayers()) do
        if plr.Character then
            if (plr.Character.PrimaryPart.Position - myPos).Magnitude < pos then
                pos = (plr.Character.PrimaryPart.Position - myPos).Magnitude
                tor = plr.Character.PrimaryPart
            end
        end
    end

    return tor
end

local function setupNPC(Bandit, BanditPosition)

    local hum = Bandit.Humanoid
    local npcPrimaryPart = Bandit.PrimaryPart
    local targetTorso
    spawn(function() 
        while wait(1) do
            targetTorso = getClosestPlayer(npcPrimaryPart.Position)
            hum:MoveTo(targetTorso.Position, targetTorso)
        end
    end)
    --hum:MoveTo(targetTorso.Position, targetTorso)

    -- clone new npc at same position
    hum.Died:Connect(function()
        wait(Settings.BANDIT.RESPAWN_TIME)
        local cln = NPCBackup:Clone()
        cln:SetPrimaryPartCFrame(BanditPosition)
        cln.Parent = workspace.Bandits
        Bandit:Destroy()
        setupNPC(cln, BanditPosition)
    end)
end

for _, Bandit in pairs(workspace.Bandits:GetChildren()) do
    spawn(function() setupNPC(Bandit, Bandit.PrimaryPart.CFrame) end)
end

This is the section where it errors:

    spawn(function() 
        while wait(1) do
            targetTorso = getClosestPlayer(npcPrimaryPart.Position)
            hum:MoveTo(targetTorso.Position, targetTorso)
        end
    end)

Error:

17:38:34.068 - ServerScriptService.NPCFollow.NPC Commander:29: attempt to index upvalue 'targetTorso' (a nil value) 17:38:34.069 - Stack Begin 17:38:34.069 - Script 'ServerScriptService.NPCFollow.NPC Commander', Line 29 17:38:34.070 - Stack End

I appreciate any feedback I get that improves this code to the best it can be or fixes my issue. Thanks for reading.

0
I wonder if it is connected that childRemoved event to every npc in the folder, so when one dies, however many npcs are in the folder that function gets called? PoePoeCannon 519 — 5y
0
I don't understand your comment. Once the child is REMOVED or DESTROYED from the folder, the event fires. So, if I detect the humanoid Died, simply destroy it and it should chain and activate the ChildRemoved Event BlackOrange3343 2676 — 5y
0
ahh, the way i see it, is that you are connecting that event function to all of the npcs in the folder, so when one gets removed all of the events fire for every npc in the folder. PoePoeCannon 519 — 5y
0
This is why you never put Events inside of loops... User#19524 175 — 5y
View all comments (10 more)
0
I will try changing the setup BlackOrange3343 2676 — 5y
0
is it ran once The_Pr0fessor 595 — 5y
0
Yes BlackOrange3343 2676 — 5y
0
You sometimes would want events in loops. hiimgoodpack 2009 — 5y
0
Help ;-; BlackOrange3343 2676 — 5y
0
Check your loop? , It might be running just once and your Event in loop is useless which results into massive collection of dead NPC in floor xD User#17685 0 — 5y
0
you can use `game:GetService("RunService").Heartbeat` if you don't want a loop TheSkyofIndia 150 — 5y
0
Thanks but do you know how to solve my current issue? I want NPC's to spawn at their original position BlackOrange3343 2676 — 5y
0
You have no error handling if nothing is in range. hiimgoodpack 2009 — 5y
0
Well it's suppose to do nothing if nothing is in range? BlackOrange3343 2676 — 5y

1 answer

Log in to vote
1
Answered by 5 years ago

From what I've looked over, the issue's that the script's attempting to get a position from targetTorso when targetTorso may (and/or is) be returning nil. A simple fix to this is to check if targetTorso return the part via a conditional.

The following code is to be used as a reference

local ReturnedTarget = GetClosestTarget()

if ReturnedTarget then
    print('Found target!')
else
    print('Target not found!')
end

If you have any questions, please let me know. I also hope this helped. :)

Ad

Answer this question