Hi, this is my first time scripting a table and I think I have added a player to the table. I’m wondering how I could call a function that assigns one of the players in the table to a variable?
Here is my script that is not working:
local plrs = {
}
local player1 = standingOnArena1Player1
local player2 = standingOnArena1Player2
table.insert(plrs, player1)
table.insert(plrs, player2)
local function getPlayer1()
for idk, getplayer in pairs(plrs) do
if getplayer == player1 then
return idk.Name
end
end
end
Return getplayer instead of idk.Name. idk is an index of the table you’re iterating through. In this case, it’s a number.
table.insert() adds a value to the table you provide for the next numerical index. I assume you’re expecting the index to be player object, but it’s the value that references the player object.
Thank you very much, sorry I have got one more question. Is this ‘player’ that is getting returned the actual player or is it a string. E.g could I say player.Character and make the player do something after assigning the variable?
For some reason, says attempted to index nil with humanoid inside connection 1 when the player dies. Am I assigning the variable wrong?
local function startEvent()
local plrs = {
}
local player1 = standingOnArena1Player1
local player2 = standingOnArena1Player2
table.insert(plrs, player1)
table.insert(plrs, player2)
local function getPlayer1()
for idk, getplayer in pairs(plrs) do
if getplayer == player1 then
return getplayer
end
end
end
local function getPlayer2()
for idk, getplayer in pairs(plrs) do
if getplayer == player2 then
return getplayer
end
end
end
local player1 = getPlayer1()
local player2 = getPlayer2()
local char1 = player1.Character
local char2 = player2.Character
game.Workspace["Custom Duels Arena"]["The Model"].Monitor.Surface.MainFrame.Main.Visible = true
game.Workspace["Custom Duels Arena"]["The Model"].Monitor.Surface2.MainFrame.Main.Visible = true
local plr1CurrentKills = 0
local plr2CurrentKills = 0
local SwordGiver = game.ServerStorage.Sword
local Sword1 = SwordGiver:Clone()
local Sword2 = SwordGiver:Clone()
local Arena1Player1Spawn = game.Workspace.Arena1Player1Spawn
local Arena1Player2Spawn = game.Workspace.Arena1Player2Spawn
print("Variables working fine")
if char1 or char2 then
local humanoid1 = player1.Character:FindFirstChild("Humanoid")
local humanoid2 = player2.Character:FindFirstChild("Humanoid")
if humanoid1 or humanoid2 then
Sword1.Parent = player1.Backpack
humanoid1:EquipTool(Sword1)
Sword2.Parent = player2.Backpack
humanoid2:EquipTool(Sword2)
print("Approved that i'm humanoid")
local connections = {}
local function disconnectConnections(kills)
if kills == 5 then
for i, v in ipairs(connections) do
v:Disconnect()
end
if plr1CurrentKills == 5 then
winner = player1
end
if plr2CurrentKills == 5 then
winner = player2
end
player1.RespawnLocation = game.Workspace.SpawnLocation
player2.RespawnLocation = game.Workspace.SpawnLocation
if winner == player1 then
Sword1:Destroy()
player1.character.HumanoidRootPart.CFrame = game.Workspace.SpawnLocation.CFrame
end
plr1CurrentKills = 0
plr2CurrentKills = 0
game.Workspace["Custom Duels Arena"]["The Model"].Monitor.Surface.MainFrame.Main.score_t1.Text = plr1CurrentKills
game.Workspace["Custom Duels Arena"]["The Model"].Monitor.Surface.MainFrame.Main.score_t2.Text = plr2CurrentKills
game.Workspace["Custom Duels Arena"]["The Model"].Monitor.Surface2.MainFrame.Main.score_t1.Text = plr1CurrentKills
game.Workspace["Custom Duels Arena"]["The Model"].Monitor.Surface2.MainFrame.Main.score_t2.Text = plr2CurrentKills
part1.Transparency = 0
part1.CanCollide = true
part2.Transparency = 0
part2.CanCollide = true
end
return true
end
connections[1] = player1.CharacterRemoving:Connect(function(hit)
local player1 = getPlayer1()
local player2 = getPlayer2()
plr2CurrentKills = plr2CurrentKills + 1
game.Workspace["Custom Duels Arena"]["The Model"].Monitor.Surface.MainFrame.Main.score_t1.Text = plr1CurrentKills
game.Workspace["Custom Duels Arena"]["The Model"].Monitor.Surface.MainFrame.Main.score_t2.Text = plr2CurrentKills
game.Workspace["Custom Duels Arena"]["The Model"].Monitor.Surface2.MainFrame.Main.score_t1.Text = plr1CurrentKills
game.Workspace["Custom Duels Arena"]["The Model"].Monitor.Surface2.MainFrame.Main.score_t2.Text = plr2CurrentKills
player2.Character.Humanoid.Health = 100
if disconnectConnections(plr2CurrentKills) then return end
wait(1)
player2.Character.HumanoidRootPart.CFrame = CFrame.new(
Arena1Player2Spawn.Position
)
player2.Character.HumanoidRootPart.CFrame = Arena1Player2Spawn.CFrame
wait(1)
local Sword1 = SwordGiver:Clone()
Sword1.Parent = player1.Backpack
end)
connections[2] = player2.CharacterRemoving:Connect(function(hit)
local player1 = getPlayer1()
local player2 = getPlayer2()
plr1CurrentKills = plr1CurrentKills + 1
game.Workspace["Custom Duels Arena"]["The Model"].Monitor.Surface.MainFrame.Main.score_t1.Text = plr1CurrentKills
game.Workspace["Custom Duels Arena"]["The Model"].Monitor.Surface.MainFrame.Main.score_t2.Text = plr2CurrentKills
game.Workspace["Custom Duels Arena"]["The Model"].Monitor.Surface2.MainFrame.Main.score_t1.Text = plr1CurrentKills
game.Workspace["Custom Duels Arena"]["The Model"].Monitor.Surface2.MainFrame.Main.score_t2.Text = plr2CurrentKills
if disconnectConnections(plr1CurrentKills) then return end
player1.Character.Humanoid.Health = 100
player1.Character.HumanoidRootPart.CFrame = CFrame.new(
Arena1Player1Spawn.Position
)
player1.Character.HumanoidRootPart.CFrame = Arena1Player1Spawn.CFrame
wait(1)
local Sword2 = SwordGiver:Clone()
Sword2.Parent = player2.Backpack
end)
end
end
end
The character doesn’t exist (as it says in the error message with “attempted to index nil”). Check if the character exists (player.Character ~= nil) first before trying to get the Humanoid instance.
I’ve added an if statement but i’m still getting the error message. Could it be the player hasn’t loaded in yet when i’m setting the variables for player1 and player2?
connections[1] = player1.CharacterRemoving:Connect(function(hit)
local player1 = getPlayer1()
local player2 = getPlayer2()
plr2CurrentKills = plr2CurrentKills + 1
game.Workspace["Custom Duels Arena"]["The Model"].Monitor.Surface.MainFrame.Main.score_t1.Text = plr1CurrentKills
game.Workspace["Custom Duels Arena"]["The Model"].Monitor.Surface.MainFrame.Main.score_t2.Text = plr2CurrentKills
game.Workspace["Custom Duels Arena"]["The Model"].Monitor.Surface2.MainFrame.Main.score_t1.Text = plr1CurrentKills
game.Workspace["Custom Duels Arena"]["The Model"].Monitor.Surface2.MainFrame.Main.score_t2.Text = plr2CurrentKills
if player1.Character ~= nil then
player2.Character.Humanoid.Health = 100
if disconnectConnections(plr2CurrentKills) then return end
wait(1)
player2.Character.HumanoidRootPart.CFrame = CFrame.new(
Arena1Player2Spawn.Position
)
player2.Character.HumanoidRootPart.CFrame = Arena1Player2Spawn.CFrame
wait(1)
local Sword1 = SwordGiver:Clone()
Sword1.Parent = player1.Backpack
end
end)