Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Why is my function returning nil?

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

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

4 comments

0

Please put Name in the scope of getPlayerTurn and not outside of it.

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?

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

0

what about putting the name as an Arg

This discussion is preserved as it was. New answers, comments, and votes are closed.