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

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

Asked by 10 years ago

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

Log in to vote
1
Answered by
2eggnog 981 Moderation Voter
10 years ago

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)
Ad
Log in to vote
1
Answered by
Gamenew09 180
10 years ago

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.

0
How could I fix it? kevinnight45 550 — 10y
0
Replace the entire script. Gamenew09 180 — 10y

Answer this question