function findTarget()
local mag = 80
local target
for v, plr in pairs(game.Players:GetPlayers()) do
local char = plr.Character
if char then
local part1 = char:FindFirstChild("Head")
local human = char:FindFirstChild("Humanoid")
if part1 and human and human.Health > 0 then
local part2 = script.Parent
local magnitude = (part1.Position - part2.Position).magnitude
if magnitude < mag then
mag = magnitude
target = part1
end
end
end
end
end
while wait() do
local part1 = findTarget()
if part1 then
while part1 and part1.Parent do
wait(20)
local insan = Instance.new("Attachment",part1)
script.Parent.Parent.Union.Beam.Attachment0 = insan
local ez = workspace.Explodey:Clone()
ez.Parent = workspace
ez.Position = part1
end
end
end
What exactly is wrong with the script?
I don’t know, it doesn’t print anything nor does it do anything.
PS This is a NPC.
Forgot to mention that
you are always setting part1 to nil because you never return anything in the function
Yeah, you forgot to add a return in the function
return target
Where do I add return target? Thanks for the responses.
function findTarget()
local mag = 80
local target
for v, plr in pairs(game.Players:GetPlayers()) do
local char = plr.Character
if char then
local part1 = char:FindFirstChild("Head")
local human = char:FindFirstChild("Humanoid")
if part1 and human and human.Health > 0 then
local part2 = script.Parent
local magnitude = (part1.Position - part2.Position).magnitude
if magnitude < mag then
mag = magnitude
target = part1
end
end
end
end
return target
end
I’ll try that. Thanks for everything, even at such a late time.
Alright so basically, this doesn’t work and I also wanted it to have a wait() feature (This NPC has a charge laser)
I’ll try mending my code. It MIGHT work, i’ll tell you if it does.
Well it doesn’t. Any ideas?
PS: Since there in different functions or something, target isn’t recognized cause it was from a different function.
Please, next time, don’t vaguely explain.
local Players = game:GetService('Players')
local RS = game:GetService('RunService')
local function FindNearestTarget(Range, Position)
local NearestTarget
local NearestDistance = 0
for _, Player in ipairs(Players:GetPlayers()) do
local Character = Player.Character
if Character and Character.Humanoid and Character.Humanoid:GetState() ~= Enum.HumanoidStateType.Dead then
local Root = Character.PrimaryPart
local Distance = (Root.Position - Position).Magnitude
if Distance >= Range and Distance >= NearestDistance then
NearestDistance = Distance
NearestTarget = Player
end
end
end
return NearestTarget
end
while true do
wait(.25)
local Target = FindNearestTarget(80, script.Parent.Position)
local Attachment = Instance.new("Attachment")
Attachment.Parent = Target
script.Parent.Parent.Union.Beam.Attachment0 = Attachment
local EFX = workspace.Explodey:Clone()
EFX.Parent = workspace
EFX.Position = Target.Position
end
Sorry, I was a bit tired of working on it (I worked like, 6 hours combined through 2 days)
(PS: I will also test this, tho it looks like it will work.)
It says attachments must be parented to a part instance on line 32.
(aka this line: Attachment.Parent = Target)
Attachment.Parent = Target:IsA("Model") and Target.PrimaryPart or workspace.Terrain
So this solved that problem, but a new problem arose (this will be the last one as this is the end of the code.)
Position is not a valid member of player players.stick_man1112, which means Target = Player not character, how do I get the character? (line 35)
PlayerService:GetPlayerFromCharacter(Target).Character
I think I got it (accidentally deleted this part)
Never mind I dont, i’m kinda clueless.
PS: It also has an attachment into Terrain for some reason.