You should use 3 backticks to make it a codeblock, compared to one backtick.
Done, thank you for the suggestion.
you have tried Boat1[Player] = nil ?, elements without values are removed.
I’ll try that out. I’ve attempted to set Boat1[i] to nil before.
I looked again at the code and I think it will not work, because Player is the value and not the index, but Boat1[i] = nil, it should work and if it does not work then the problem is elsewhere.
Can you show us how you are inserting the player into the table?
I quickly setup a simple script and it works fine and should be about the same as what you’re doing.
local Players = game:GetService("Players")
local Table = {}
Players.PlayerAdded:Connect(function(Player)
table.insert(Table, #Table + 1, Player)
end)
Players.PlayerRemoving:Connect(function(Player)
for i = 1, #Table do
if Table[i] == Player then
print(Table[i])
table.remove(Table, i)
print(Table[i])
end
end
end)
When the player leaves the output is:
CleverSource
nil
this is correct as I print the player before I remove them from the table, I remove them, and then print the index of the table again and it is nil
.
Are you sure the values stored in the tables are player objects and not the player’s usernames (strings)?
Also, if you’re just referencing the values within a table, you could do
for i, v in pairs(Boat1) do
-- code
end
where the variable “i” maintains the same value as your variable “i”, but “v” maintains the value of “Boat1[i]”.
Also you are receiving the warning in output?
I think you’ve mistaken how this code works. Boat1[i] would return the value in Boat1 with the index of the variable i.
E.g.
If the items within Boat1 would be {“hello”,“there”,“my”,“name”,“is”,“bob”}
and the value of i would be 3,
print(Boat1[i]) would output the string “my”.
Needless to say it, I already commented my mistake above.
Sure.
Oh, I see my mistake. I’m storing the player object into the table, silly me.
Nothing related to this chunk of code is showing in the output.
Storing the player object is fine though. That should not be a mistake. If you review the sample code I’ve provided, I am storing the player object, the sample code I am referring to is here.
Not sure how storing player objects is affected by when players leave, but if you’re going to change it to store the player’s usernames, make sure to also compare them to the player’s username and not the object.
table.insert(Boat1, Player)
table.insert(Boat2, Player)
table.insert(Boat3, Player)
That is your error. You need to do table.insert(Boat1, #Boat1 + 1, Player)
and so on. table.insert
takes 3 parameters, the table which you want to add the value to, the index you wish to add it to, and the value you want to add.
Ah, I see it now, thank you for the help.
Is that necessary? I’ve never used that with table inserting before (unless I wanted a specific position), it’s always automatically appended for me.
EDIT: The question isn’t sarcastic but more out of pure interest
table.insert can be used with two parameters, and it will do the same that you said.
https://gyazo.com/08e1c322613b7f8b0beac698bb22440b
Thank you, the code is working now! I’m in need of practice with tables. If you can, could you possibly link a few sources that could help?
Hmm, you’re correct, was an oversight on my part. I don’t often use table.insert
but yes, it does automatically append if you don’t provide an index.
There could be a more underlying issue here. @R0BL0XIAN_D3M0 can you possibly provide the entire script?
Edit: Never mind it looks like my solution solved it.