Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How can I share a Players Specific Character between Scripts?

Making a game where you can spawn in noobs to fight other players but I don't want the noobs attacking its owners and I don't know how I can transfer information of a players character so I can make that possible

Here is the script in to noobs

wait(2)
function findNearestTorso(pos)
    local list = game.Workspace:children()
    local torso = nil
    local dist = 99999999999999
    local temp = nil
    local human = nil
    local temp2 = nil
    for x = 1, #list do
        temp2 = list[x]
        if (temp2.className == "Model") and (temp2 ~= script.Parent) then
            temp = temp2:findFirstChild("Torso")
            human = temp2:findFirstChild("Humanoid")
			if (temp ~= nil) and (human ~= nil) and (human.Health > 0) then
                if (temp.Position - pos).magnitude < dist then
                    torso = temp
                    dist = (temp.Position - pos).magnitude
                end
            end
        end
    end
    return torso
end
--wait(math.random(0,5)/10)
while wait(0.1) do
	script.Parent.Human.Died:Connect(function()
		script.Parent:Destroy()
	end)
	if script.Parent.Human.Health == 0 then
		script.Parent:Destroy()
	end
    local target = findNearestTorso(script.Parent. Torso.Position)
    if target ~= nil then
        script.Parent.Human:MoveTo(target.Position, target)
    end
end

3 comments

0

There is a lot wrong with this...

0

no shit

0

Can't you do "If humanoid.Parent == owner then"

1 answer

This appears to be a template for an old zombie pursuit script, so I can safely assume that this is, quite frankly, not your own code. However, before I answer your question, your code needs fixing. A lot of fixing.

  • children() is a deprecated function (deprecated != unusable). You should use GetChildren().
  • Instead of checking for ClassName, use IsA(className).
  • FindFirstChild() over findFirstChild() (PascalCase over camelCase) and Magnitude over magnitude.
  • Do not loop event listeners (the Died event in your case). This creates an unnecessarily large number of event listeners that can lead to lag.
  • Also, do not use the while wait() do idiom if you have no clue what it does.

Now for the answer to your original question: How do I prevent NPCs from targeting other NPCs?

The Players service offers a function called GetPlayerFromCharacter(). This returns a player if the given Instance is associated with a client, or nil if the Instance is not associated with a client. Here is an example:

local plrs = game:GetService("Players")
for _, item in ipairs(workspace:GetChildren()) do
	if plrs:GetPlayerFromCharacter(item) then
		print("Player found")
	else
		print("No player found")
	end
end

Assuming there is one character in the workspace associated with the only player in-game, there will be one output of Player found, and every other output will be No player found.

This discussion is preserved as it was. New answers, comments, and votes are closed.