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

Why is my function returning nil?

Asked by
VicyX 27
5 years ago
Edited 5 years ago

I have this function that loops through a folder that hold the players turn order via string values named 'Player1', 'Player2', 'Player3',etc. all the way up to 'Player8'. If the value is empty then the value is removed via a different script. but thats not the issue. Whenever i check for the players name then return the players name and attempt to print the function i get the error ServerStorage.doTurn:31: attempt to concatenate a nil value

This is the function:

local RS = game:GetService("ReplicatedStorage")
local TurnNum = RS.Turn

local function getPlayerTurn()
    for i,v in pairs(RS.PlayerOrder:GetChildren()) do
        print(TurnNum.Value)
        if v.Name == 'Player'.. tostring(TurnNum.Value) then
            print(v.Name)
            return v.Name
        else
            TurnNum.Value = TurnNum.Value + 1
            wait()
            getPlayerTurn()
        end
    end
end

print(getPlayerTurn().."'s turn")

EDIT: TurnNum gets set to 1 when it reaches 9(making it range from 1 - 8)

1 answer

Log in to vote
0
Answered by 5 years ago
Edited 5 years ago

So Im not completely sure but try

local RS = game:GetService("ReplicatedStorage") local TurnNum = RS.Turn

local Name
local function getPlayerTurn()
    for i,v in pairs(RS.PlayerOrder:GetChildren()) do
        print(TurnNum.Value)
        if v.Name == 'Player'.. tostring(TurnNum.Value) then
            print(v.Name)
            Name = v.Name
        else
            TurnNum.Value = TurnNum.Value + 1
            wait()
            getPlayerTurn()
        end
    end
    return Name
end

print(getPlayerTurn().."'s turn")

making the name a value but as previously stated i'm not completely sure

0
Please put `Name` in the scope of `getPlayerTurn` and not outside of it. EpicMetatableMoment 1444 — 5y
0
Name does indeed server no purpose being out of the for loop. Instead have it end the loop prematurely by returning in it. Also doesn't this have to potential to stack overflow? NLferdiNL 21 — 5y
0
I tried what you did and no matter what I do it continues to return nil. I even have it print 'Name' and it prints the correct name but it returns nil. Idk if return is broken or what VicyX 27 — 5y
0
what about putting the name as an Arg The_Pr0fessor 595 — 5y
Ad

Answer this question