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

Function not returning properly?

Asked by 5 years ago
Edited 5 years ago
function funcNear()
local nearestPlr 
    for _, val in pairs(game.Players:GetPlayers()) do
        local char = val.Character
        if char then 
            local dist = val:DistanceFromCharacter(script.Parent.HumanoidRootPart.Position)
            if dist < script.Parent.Configuration.TriggerDistance.Value then
                local nearestPlr = char
                print(nearestPlr)
            end
        end
    end
    return nearestPlr
end

while wait(3) do
    local x = funcNear()

    print(x)
end

nearestPlr is just returning nil when being called.

1
You have to identify what nearestPlr is. Asher0606 36 — 5y
0
If no character is closer than `script.Parent.Configuration.TriggerDistance.Value`, then `nearestPlr` will never be assigned to. Also, if multiple characters are close enough, it will pick the first one it finds, rather than the one which is *actually* closer. fredfishy 833 — 5y
0
yeah im aware, this was mainly to test returning a value to another modulescript yhatayhatahoohoo 15 — 5y

1 answer

Log in to vote
0
Answered by 5 years ago

have you realized you created nearestPlr twice?

once in line 2, local nearestPlr

twice in line 8, local nearestPlr = char

that is two different variables!!!!

by the time it reach line 13, the second variable is out of scope, it is returning the value in first variable created in line 2. oops. you have never set that variable in the first place.

just remove "local " in line 8

1
Thats what I told him. I guess, I have to be more specific next time. Asher0606 36 — 5y
Ad

Answer this question