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

how do i return values?

Asked by 5 years ago
Edited 5 years ago

hello, i am new to returning and i need some help with it. i am currently making an AI and i need some help with returning two variables, the target humanoid and the target humanoid root part. i want these two values to be returned to a new function but it says the value i am trying to access is unknown, this probably means it is a problem with scope, which i am also not very good at.

01local animatronic = script.Parent
02local animatronicHumanoidRootPart = animatronic:FindFirstChild("HumanoidRootPart")
03local animatronicHumanoid = animatronic:FindFirstChild("Humanoid")
04 
05local function findTarget()
06    print(1)
07    for i, players in pairs (game.Players:GetPlayers()) do
08        local targetCharacter = players.Character or players.CharacterAdded:Wait()
09        if targetCharacter then
10            local targetHumanoidRootPart = targetCharacter:FindFirstChild("HumanoidRootPart")
11            local targetHumanoid = targetCharacter:FindFirstChild("Humanoid")
12            if targetHumanoidRootPart then
13                if targetHumanoid then
14                    if (targetHumanoidRootPart.Position - animatronicHumanoidRootPart.Position). magnitude < 100 and targetHumanoid.Health > 0 then
15                        return targetHumanoid, targetHumanoidRootPart -- returning the values. im not sure how to return multiple values, i assume this is how you do it though
View all 35 lines...

1 answer

Log in to vote
1
Answered by
Ziffixture 6913 Moderation Voter Community Moderator
5 years ago
Edited 5 years ago

This doesn’t correlate to a returning issue, you’ve used the keyword correctly, even with numerous return values. The issue at hand is that you’re passing an argument into a function with no parameters set; essentially, it’s not needed. You never declared targetHumanoid either, so you’re trying to pass in something "unknown".

The way a return will function when handling multiple values, is that it will push each value in respective order to the containers provided, meaning there needs to be as many available variables as there are values being given back, otherwise some values will be wasted and lost. This isn’t entirely a bad thing however.

1local targetHumanoid, targetRoot = findTarget()
2print(targetHumanoid, targetRoot)

This is all you need.

If you have any more questions, or this didn’t solve your issue, please comment below, otherwise, don’t forget to **accept* this answer!

0
i changed this ~~~~~~~~ local targetHumanoid, targetRoot = findTarget(targetHumanoid, targetHumanoidRootPart) ~~~~~~~~~~~ and it still comes up with the Unknown error in the output Code1400 75 — 5y
Ad

Answer this question