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

Why won't this function return a argument and how do I fix it?[Not fixed]

This script won't do anything even when the right player enters (Doesn't work in solo also)and i'm guessing its because of line 3. Oh,and how could I fix it?

PlayerWithTools = {"Zombomafoo","Player1"}
--------------------------------------------------
function FindFirstInTable(Table,String)
    for _,v in pairs (Table) do
        if v == String then
            return v,true
        else 
            return false
        end
    end
end
--------------------------------------------------
game.Players.PlayerAdded:connect(function(Player)
    Player.CharacterAdded:connect(function(Character)
----------------------------------------------------
            Player = FindFirstInTable(PlayerWithTools,Player.Name)
                if Player then
                    for _,Tools in pairs (game.ServerStorage:GetChildren()) do
                    ClonedTools = Tools:Clone()
                        ClonedTools.Parent = Player.Backpack
            end
        end
    end)
end)

2 answers

✓ Accepted answer

I think I got it to work, but Roblox is down, so I couldn't test it. I explained your mistakes in the comments.

PlayerWithTools = {"Zombomafoo","Player1"}
--------------------------------------------------
function FindFirstInTable(Table,String)
    for _,v in pairs (Table) do
        if v == String then
            return v,true
        end
    end
    return false --The other guy was right about this; it was returning false prematurely.
end
--------------------------------------------------
game.Players.PlayerAdded:connect(function(Player)
	Player.CharacterAdded:connect(function(Character)
		PlayerMatch = FindFirstInTable(PlayerWithTools,Player.Name) --This was originally replacing your Player variable with the string returned from FindFirstInTable. Further down, it was attempting to clone the tools into the string rather than the player.
		if PlayerMatch then
			for _,Tools in pairs(game.ServerStorage:GetChildren()) do
				ClonedTools = Tools:Clone()
				ClonedTools.Parent = Player.Backpack
			end
		end
	end)
end)

I see your problem, after the first time a person doesn't meet the requirements it returns false which causes it to return false prematurely.

PlayerWithTools = {"Zombomafoo","Player1"}
--------------------------------------------------
function FindFirstInTable(Table,String)
    for _,v in pairs (Table) do
        if v == String then
            return v,true
        end
    end
    return false -- Outside of the for loop should return false, so it can iterate through the table.
end
--------------------------------------------------
game.Players.PlayerAdded:connect(function(Player)
    Player.CharacterAdded:connect(function(Character)
----------------------------------------------------
            PlayerMatch = FindFirstInTable(PlayerWithTools,Player.Name) -- Thanks 2eggnog for pointing this out, I didn't even notice.
                if PlayerMatch then
                    for _,Tools in pairs (game.ServerStorage:GetChildren()) do
                    ClonedTools = Tools:Clone()
                        ClonedTools.Parent = Player.Backpack
            end
        end
    end)
end)

Did I help? Please vote me up, it helps a lot. Thanks.

2 comments

0

How could I fix it?

0

Replace the entire script.

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