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

Why does this table.insert and table.getn script not print testtesttest?

I am trying to make a part that when touched, adds the players names to the table (For now I'm testing with just adding test with a value of test2 and I want it to print "testtesttest" but the game prints "Workspace.EnterSensor.Script:7:wrong number of arguments to 'insert' " and doesn't print anything, could someone please tell me why this is happening?

Here is the script (It's a server script located inside of a part which is located in the workspace):

PlayersTouching = {}
		
script.Parent.Touched:connect(function(hit)
    if hit.Parent:FindFirstChild("Humanoid") then
        local plr = workspace[hit.Parent.Name] --Access character from workspace.
        local player = game.Players:GetPlayerFromCharacter(plr) --Access (plr)
		table.insert(PlayersTouching, 1, "Test", "test2")
		if table.getn(PlayersTouching, 1) == "test2" then
			print("testtesttest")
		end
		
    end
end)

Please tell me if you have any idea why this is happening!

1 comment

0

getn is deprecated

1 answer

✓ Accepted answer

table.insert has 2 parameters.

table.insert(TABLENAME,VALUE)

There is also a third optional parameter

table.insert(TABLENAME, POSITION IN TABLE, VALUE)

You've put in 4 arguments, hence the error you've received.

Edit: Removed deprecated stuff due to being yelled at.

PlayersTouching = {}

script.Parent.Touched:Connect(function(hit)
    if hit.Parent:FindFirstChild("Humanoid") then
        local plr = workspace[hit.Parent.Name] --Access character from workspace.
        local player = game.Players:GetPlayerFromCharacter(plr) --Access (plr)
        table.insert(PlayersTouching, 1, "Test")
        if PlayersTouching[1] == "Test" then
            print("testtesttest")
        end
    end
end)

3 comments

0

You should point out the other mistakes too.

0

Do i gotta

0

Thank You :)

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