Why is it not returning true in a simple for I, v loop in a function?

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    return the first value when the condition is met
  2. What is the issue? Include screenshots / videos if possible!
    the value is not returning although the condition is met
 local players = game:GetService('Players')
local event = game:GetService("ReplicatedStorage").events.spawncharacter
local peoplefolder = game.Workspace.CompassPeople:GetChildren()
players.PlayerAdded:Connect(function(plr)
	
	
	local boolvalue = Instance.new("BoolValue")
	local boolvalue2 = Instance.new("BoolValue")
	boolvalue2.Value = false
	boolvalue.Value = false
	boolvalue.Name = "playerisnear"
	boolvalue2.Name = "playerplacedcharacter"
	boolvalue2.Parent = plr
	boolvalue.Parent = plr

end)


	
	
	local function characterloaded(PlayerName:string?)
		for _,v in peoplefolder do
			if tostring(v):match(PlayerName) then
				return true
			end
		end
		return false
	end






event.OnServerEvent:Connect(function(player,mousepos,mousetarget)
	
	
	print(player.Name)

	if 	characterloaded(player.Name) == false and mousetarget.Name == "PlayerTile" and player:WaitForChild("playerplacedcharacter").Value == false then
		player:WaitForChild("playerplacedcharacter").Value = true
		print("it is false")
		print(mousepos)
		local charactercopy = game.Players:CreateHumanoidModelFromUserId(player.UserId)
		charactercopy.Name = player.Name
		charactercopy.Parent = game.Workspace.CompassPeople
		charactercopy.PrimaryPart.CFrame = mousepos + (charactercopy.PrimaryPart.CFrame).LookVector + Vector3.new(0,4,0)
		
	elseif characterloaded(player.Name) == true and mousetarget.Name == "PlayerTile" and player:WaitForChild("playerplacedcharacter").Value == true then
	
		print("it is true")
	end

	
	
	
	
	
	
	
	
	
	
	
	
	
end)

what is peoplefolder?

a folder of player character models

I already settled the variable for it with get children

And how do you get them? Plus what triggers the function?

when I call a function then this function fires and the parameter is the player’s name

Try this.

local function CharacterLoaded(PlayerName:string?)
	for _,v in peoplefolder do
		if tostring(v):match(PlayerName) then
			return true
		end
	end
	return false
end

There’s no else before the return false, so either way, it returns false.

It doesn’t matter. It should return true then stop the function. If any of the string matches, the loop stops thus causing it to return false.

2 Likes

I will just send the full code

You can just send the connection that triggers the function and peoplefolder.

local function CharacterLoaded(PlayerName:string?)
	for _,v in peoplefolder do
		if tostring(v):match(tostring(PlayerName)) then
			return true
		end
	end
	return false
end

Actually just use this, it converts everything to a string so it works. Because I would assume PlayerName isn’t a string. Additionally you can use this.
print(type(PlayerName))

for me true is still not returning

Debug.

local function CharacterLoaded(PlayerName:string?)
	print(type(PlayerName))
	for _,v in peoplefolder do
		print(type(v))
		if tostring(v):match(tostring(PlayerName)) then
			return true
		end
	end
	return false
end

Tell me everything that prints.

when I fire the function first, it returns the false correctly but when I fire it again, it returns the player name once, and user data twice

Can I see how you get peoplefolder children or its variable name?

if you look at the beginning of this convo I edited my code with the whole code

PeopleFolder will only run ONCE. Meaning you will only have the old default folder and not any updated folder.

local function characterloaded(PlayerName:string?)
	for _,v in game.Workspace.CompassPeople:GetChildren() do
		if tostring(v):match(PlayerName) then
			return true
		end
	end
	return false
end

Now it works. Thank you!!! :smiley:

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.