Hello!
I have got a problem in my script where a table is in one function but a table.remove is in another.
Example:
Lets say we have got a table called: mytable
And mytable
is in a function:
game.Players.PlayerRemoving:Connect(function(plr)
local mytable = {
"cat",
"dog",
"attack helicopter"
}
end)
But we have another function where we want to remove something from that table like this:
game.Players.PlayerAdded:Connect(function(plr)
table.remove(mytable, "dog")
end)
But roblox studio doesnât recognise mytable
in the PlayerAdded
function.
How do we fix this so it will recognise mytable
in another function?
Full Example Script (Server)
game.Players.PlayerAdded:Connect(function(plr)
table.remove(mytable, "dog")
end)
game.Players.PlayerRemoving:Connect(function(plr)
local mytable = {
"cat",
"dog",
"attack helicopter"
}
end)
2 Likes
If you want the table to be identical for all players, you could move the declaration outside of the connections:
local mytable = {
"cat",
"dog",
"attack helicopter"
}
game.Players.PlayerAdded:Connect(function(plr)
table.remove(mytable, "dog")
end)
game.Players.PlayerRemoving:Connect(function(plr)
end)
If, however, you want it to be specific to the player, then I would recommend having a dictionary (with the player as the key):
local Tables = {}
game.Players.PlayerAdded:Connect(function(plr)
table.remove(Tables[plr], "dog")
end)
game.Players.PlayerRemoving:Connect(function(plr)
Tables[plr] = {
"cat",
"dog",
"attack helicopter"
}
end)
2 Likes
An error appears.
18:21:45.789 ServerScriptService.ToolSaving:40: invalid argument #1 to âremoveâ (table expected, got nil) - Server - ToolSaving:40
On this line:
table.remove(tools[plr], tool)
here is the table
local tools = {}
Can you please show me the entirety of the code?
local Players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")
local ServerStorage = game:GetService("ServerStorage")
local RepliStorage = game:GetService("ReplicatedStorage")
local player_data = DataStoreService:GetDataStore("TestData1")
local RemoveSoldItem = RepliStorage.Remotes.RemoveSoldItem
local toolsInstance = ServerStorage.Tools
local tools = {}
Players.PlayerAdded:Connect(function(client)
client.CharacterAdded:Connect(function(plr)
local key = "client_" .. client.UserId
local inventory = player_data:GetAsync(key)
print(inventory)
for i, name in inventory or {} do
local tool = game:GetService("ReplicatedStorage").ItemAssets:FindFirstChild(name)
local Backpack = client.Backpack
if tool then
print(tool.Name)
local Clonedtool = tool:Clone()
Clonedtool.Name = tool.Name
Clonedtool.Parent = Backpack
print("tool cloned")
else
warn("Item could not be cloned")
end
end
RemoveSoldItem.OnServerEvent:Connect(function(plr, tool)
local Backpack = client.Backpack
local tool = Backpack:FindFirstChild(tool)
table.remove(tools[plr], tool)
print("Removed "..tool)
end)
end)
end)
Players.PlayerRemoving:Connect(function(client)
local key = "client_" .. client.UserId
tools[client] = { }
local Character = client.Character
if Character then
for X, item in Character:GetChildren() do
if not item:IsA("Tool") then continue end
table.insert(tools[client], item.Name)
end
end
for _, item in client.Backpack:GetChildren() do
table.insert(tools, item.Name)
end
player_data:UpdateAsync(key, function(prev)
return tools
end)
end)
1 Like
At no point are you defining tools[player]
(or tools[client]
). You need to define it with the default values at some point within the PlayerAdded command
1 Like
What is your problem currently?
1 Like
Ive got this error:
19:17:25.980 ServerScriptService.ToolSaving:25: invalid argument #2 (string expected, got table) - Server - ToolSaving:25
on this line:
local tool = game:GetService("ReplicatedStorage").ItemAssets[name]
Full (server) script:
local Players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")
local ServerStorage = game:GetService("ServerStorage")
local RepliStorage = game:GetService("ReplicatedStorage")
local player_data = DataStoreService:GetDataStore("TestData1")
local RemoveSoldItem = RepliStorage.Remotes.RemoveSoldItem
local toolsInstance = ServerStorage.Tools
local tools = {}
Players.PlayerAdded:Connect(function(client)
client.CharacterAdded:Connect(function(plr)
tools[client] = {}
local key = "client_" .. client.UserId
local inventory = player_data:GetAsync(key)
print(inventory)
for i, name in inventory do
print(name)
local tool = game:GetService("ReplicatedStorage").ItemAssets[name]
local Backpack = client.Backpack
print(tool)
if tool then
print(tool.Name)
local Clonedtool = tool:Clone()
Clonedtool.Name = tool.Name
Clonedtool.Parent = Backpack
print("tool cloned")
else
warn("Item could not be cloned")
end
end
RemoveSoldItem.OnServerEvent:Connect(function(plr, tool)
local Backpack = client.Backpack
local tool = Backpack:FindFirstChild(tool)
table.remove(tools[client], tool)
print("Removed "..tool)
end)
end)
end)
Players.PlayerRemoving:Connect(function(client)
local key = "client_" .. client.UserId
tools[client] = { }
local Character = client.Character
if Character then
for X, item in Character:GetChildren() do
if not item:IsA("Tool") then continue end
table.insert(tools[client], item.Name)
end
end
for _, item in client.Backpack:GetChildren() do
table.insert(tools[client], item.Name)
end
print(tools[client])
player_data:UpdateAsync(key, function(prev)
return tools
end)
end)
1 Like
You should use FindFirstChild()
method.
its table.remove(mytable, table.find(mytable, âdogâ))
Like this:
local tool = game:GetService("ReplicatedStorage").ItemAssets:FindFirstChild(name)
I will be back in 15 mins.
guys if the error says it got a table, that means the given value is a table.
table.find() doesnât return a table
:FindFirstChild() doesnât stop a table from being passed
name
must be a table
check the data thatâs being printed Planey
Here is what appeared in my output
19:30:36.079 argument #1 expects a string, but table was passed - Server - ToolSaving:25
Then, that may be a table I think.
Yes it is but it is supposed to be a string. How do I fix this?
The (or an) issue is on line 72. You are returning tools when you should be returning tools[client].