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 4 years ago
Edited 4 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.

local animatronic = script.Parent
local animatronicHumanoidRootPart = animatronic:FindFirstChild("HumanoidRootPart")
local animatronicHumanoid = animatronic:FindFirstChild("Humanoid")

local function findTarget()
    print(1)
    for i, players in pairs (game.Players:GetPlayers()) do
        local targetCharacter = players.Character or players.CharacterAdded:Wait()
        if targetCharacter then
            local targetHumanoidRootPart = targetCharacter:FindFirstChild("HumanoidRootPart")
            local targetHumanoid = targetCharacter:FindFirstChild("Humanoid")
            if targetHumanoidRootPart then
                if targetHumanoid then
                    if (targetHumanoidRootPart.Position - animatronicHumanoidRootPart.Position). magnitude < 100 and targetHumanoid.Health > 0 then
                        return targetHumanoid, targetHumanoidRootPart -- returning the values. im not sure how to return multiple values, i assume this is how you do it though

                    end
                end
            end
        end
    end
end

local function noTarget()
    findTarget() -- the value function thats returning the values

end

local function followTarget()
    local targetHumanoid = findTarget(targetHumanoid) -- accessing the variables gives me the unknown error, im only trying to access one at the moment
end

while wait(5) do

end

1 answer

Log in to vote
1
Answered by
Ziffixture 6913 Moderation Voter Community Moderator
4 years ago
Edited 4 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.

local targetHumanoid, targetRoot = findTarget()
print(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 — 4y
Ad

Answer this question