Hello Developers.
As the title says, how can I detect if a humanoid NPC has no tag?
Hello Developers.
As the title says, how can I detect if a humanoid NPC has no tag?
Can you explain more on what you mean by “tag?”
Do you mean tag as in how CollectionService uses tags?
Yeah, I think? Basically, I got this batter handler script in ServerScriptService. Anyway, the script also controls the movement of the battler depending if they are an enemy or not (Enemies move right while friendly battlers move right).
I want it so that the battler stops moving if they don’t have a tag or doing an action (such as fighting). I also want it to move in opposite directions if their tag got changed. (For example, if an enemy battler somehow becomes a friendly battler, it will move to the right rather than moving to the left.)
EDIT: I forgot the script woops
local RP = game:GetService("ReplicatedStorage")
local BATTLERSFOLDER = RP:WaitForChild("BATTLERS")
local SUMMON_UNITEVENT = RP:WaitForChild("SUMMON_UNIT")
--//Friendly
SUMMON_UNITEVENT.OnServerEvent:Connect(function(plr, Wishedunit)
local ifUnitExist = BATTLERSFOLDER:FindFirstChild(Wishedunit)
if ifUnitExist then
local existingBattler = ifUnitExist:Clone()
--Positioning
existingBattler.Parent = workspace.ExistingEntities
existingBattler:WaitForChild("HumanoidRootPart").CFrame = workspace.Spawn.CFrame + Vector3.new(0, 3, 0)
--Team
existingBattler:AddTag("FriendlyTag")
for _, bodypart in pairs(existingBattler:GetChildren()) do
if bodypart:IsA("BasePart") then
bodypart.CollisionGroup = "NPCs"
end
end
--existingBattler.Humanoid:Move(Vector3.new(-1, 0, 0))
elseif not ifUnitExist then
warn(plr.Name.." tried requesting a non-existant battler.")
end
end)
--//Enemies
--TBA
--//Movement
while task.wait(0.5) do
for _, Entities in pairs(workspace.ExistingEntities:GetChildren()) do
if Entities:IsA("Model") then
local Hum = Entities:WaitForChild("Humanoid")
if Entities:HasTag("FriendlyTag") then
Entities.Humanoid:Move(Vector3.new(-1, 0, 0))
elseif Entities:HasTag("EnemyTag") then
Entities.Humanoid:Move(Vector3.new(1, 0, 0))
end
end
end
end
To see if an Instance has no tags, do
if #Instance:GetTags() == 0 then
Did I do it correctly?
--//Movement
while task.wait(0.5) do
for _, Entities in pairs(workspace.ExistingEntities:GetChildren()) do
if Entities:IsA("Model") then
local Hum = Entities:WaitForChild("Humanoid")
if #Entities:GetTags() == 0 then
Hum:Move(Vector3.new(0, 0, 0))
else
if Entities:HasTag("FriendlyTag") then
Entities.Humanoid:Move(Vector3.new(-1, 0, 0))
elseif Entities:HasTag("EnemyTag") then
Entities.Humanoid:Move(Vector3.new(1, 0, 0))
end
end
end
end
end
Yeah that would be correct. Should work as expected
When I try removing the battler’s FriendlyTag via the Command Bar when testing, it didn’t work as he kept moving.
Make sure you’re doing that on the server, or whatever context your script is.
The handler for that script is in ServerScriptService.
As mentioned, make sure you’re in server mode when executing the command in the command bar. If you’re not sure that’s the way to check if an Instance has no tags, call
print(#workspace:GetTags())
or on any other non-tagged Instance.
Or you could add an else
that should run if the NPC doesn’t have any of the specified tags.
How can I do that?
{filller aaaaaay}
Just… add an else
case in that if
block that runs this?
So like this?
while task.wait(0.5) do
for _, Entities in pairs(workspace.ExistingEntities:GetChildren()) do
if Entities:IsA("Model") then
local Hum = Entities:WaitForChild("Humanoid")
if Entities:HasTag("FriendlyTag") then
Entities.Humanoid:Move(Vector3.new(-1, 0, 0))
elseif Entities:HasTag("EnemyTag") then
Entities.Humanoid:Move(Vector3.new(1, 0, 0))
else
Entities.Humanoid:Move(Vector3.new(0, 0, 0))
end
end
end
end
Yes. Now test the NPCs and see if that works
Sadly doesn’t work.
{djqhueqowheuqqhieuhiqewuhewui filler}
Add a print in each of the checks to see if they work. Add a print in the loop and above to see if it runs.
Dammit, it still won’t work.
{god damn this stupid filler}
Can you share the current code, with prints, and tell which ones get in the output?
I’ll send you the code for it later.