So I am making a loop kill admin command, which is a sort of troll command, but the code isn’t working, I have searched on the wiki and I cannot find anything. (I put my username in the table to test but in the actual game, I use table.insert to insert the player name once the command is fired).
local loopkillPlayers = {"WinkleSociety"}
------------------------
--Loop killing players--
------------------------
game.Players.PlayerAdded:Connect(function(player)
player.CharacterAdded:Wait()
for i, name in pairs(loopkillPlayers) do
if player.Name == name then
local char = player.Character
if char then
if char.Humanoid.Health > 0 then
char.Humanoid.Health = 0
end
end
end
end
end)
game.Players.PlayerRemoving:Connect(function(player)
for _, item in pairs(loopkillPlayers) do
if player.Name == item then
table.remove(loopkillPlayers,item)
end
end
end)
(I also cannot find a way to get table.remove to work).
local Players = game:GetService("Players")
Players.PlayerAdded:Connect(function(plr)
plr.Chatted:Connect(function(msg)
if msg:sub(1, #":loopkill"):lower() == ":loopkill" then
local name = string.gsub(msg, ":loopkill ", "")
local function findPlayer()
for i,v in ipairs(Players:GetPlayers()) do
if v.Name:lower() == name:lower() then
return v
end
end
end
local plrToLoopKill = findPlayer()
print("Player to loop kill: " .. plrToLoopKill.Name)
local charToLoopKill = plrToLoopKill.Character or plrToLoopKill.CharacterAdded:Wait()
plrToLoopKill.CharacterAdded:Connect(function(characterThatGotAdded)
wait(0.5)
characterThatGotAdded.Humanoid.Health = 0
end)
charToLoopKill.Humanoid.Health = 0
end
end)
end)
You don’t need tables.
If you want to make a command that stops the loopkill, you could store the connections in a table though and then just disconnect it.
You’re trying to kill the character once, and only when the player joins the game. You should instead use the CharacterAdded event, which would run every time the character respawns. Then instead of an array you can use a dictionary, where keys would be player names, and values would be CharacterAdded connections.
Also another way to kill a character is to call BreakJoints() function.
-- Reusing same function
local function KillCharacter(char)
wait(0.5)
char:BreakJoints()
end
local loopkillPlayers = {}
function LoopKill(Target)
-- Storing the connection in the dictionary
loopkillPlayers[Target.Name] = Target.CharacterAdded:Connect(KillCharacter)
if Target.Character then
KillCharacter(Target.Character)
end
end
function UnLoopKill(Target)
local connection = loopkillPlayers[Target.name]
if connection then
-- Disconnecting the CharacterAdded event from the player
loopkillPlayers[Target.Name] = nil
connection:Disconnect()
end
end
Side note: table.remove needs an index, not an item. And you cannot use it in pairs loop, since removing an item will skip the next item iteration.
local Players = game:GetService("Players")
local connections = {}
local function findPlayer(name)
for i,v in ipairs(Players:GetPlayers()) do
if v.Name:lower() == name:lower() then
return v
end
end
end
Players.PlayerAdded:Connect(function(plr)
plr.Chatted:Connect(function(msg)
if msg:sub(1, #":loopkill"):lower() == ":loopkill" then
local name = string.gsub(msg, ":loopkill ", "")
local plrToLoopKill = findPlayer(name)
local c
local charToLoopKill = plrToLoopKill.Character or plrToLoopKill.CharacterAdded:Wait()
c = plrToLoopKill.CharacterAdded:Connect(function(characterThatGotAdded)
wait(0.5)
characterThatGotAdded.Humanoid.Health = 0
end)
charToLoopKill.Humanoid.Health = 0
connections[plrToLoopKill.Name] = c
elseif msg:sub(1, #":unloopkill"):lower() == ":unloopkill" then
local name = string.gsub(msg, ":unloopkill ", "")
local plrToUnLoopKill = findPlayer(name)
if connections[plrToUnLoopKill.Name] then
connections[plrToUnLoopKill.Name]:Disconnect() --Disconnecting the event
connections[plrToUnLoopKill.Name] = nil --Deleting the entry
end
end
end)
end)