##help Fix Team Kill

1 messages · Page 1 of 1 (latest)

dusky fern
#

Can anyone help me fix team kill if possible?

function onHumanoidDied(humanoid, player)
local killer = getKillerOfHumanoidIfStillInGame(humanoid)
if (killer ~= nil) then
handleTKs(player, killer)
end
end

function onPlayerRespawn(property, player)
-- need to connect to new humanoid

if property == "Character" and player.Character ~= nil then
    local humanoid = player.Character.Humanoid
    local p = player
    local h = humanoid
    humanoid.Died:connect(function() onHumanoidDied(h, p) end )
end

end

function getKillerOfHumanoidIfStillInGame(humanoid)
-- returns the player object that killed this humanoid
-- returns nil if the killer is no longer in the game

-- check for kill tag on humanoid - may be more than one - todo: deal with this
local tag = humanoid:findFirstChild("creator")

-- find player with name on tag
if tag ~= nil then
    local killer = tag.Value
    if killer.Parent ~= nil then -- killer still in game
        return killer
    end
end

return nil

end

function handleTKs(player, killer)
if (killer.TeamColor == player.TeamColor) then
killer.Character:MoveTo( Vector3.new(-308, 3.4, 129) ) -- Insert Here The Position of the Noob House
wait(30)
killer.Character.Humanoid.Health = 0
wait(7)
killer.Character.Humanoid.Health = 0
end
end

function onPlayerEntered(newPlayer)
while true do
if newPlayer.Character ~= nil then break end
wait(3)
end

local humanoid = newPlayer.Character.Humanoid

humanoid.Died:connect(function() onHumanoidDied(humanoid, newPlayer) end )
newPlayer.Changed:connect(function(property) onPlayerRespawn(property, newPlayer) end )

end

game.Players.ChildAdded:connect(onPlayerEntered)

queen flame
#

Format your code bro

dusky fern
queen flame
dusky fern
queen flame
#

Also wdym "help fix team kill" if your code works as u wrote it

dusky fern
#

thanks for ur help.

vague kindle
#

format it like that so we can read it better

dusky fern
#

so better format will fix the team kill?

queen flame
dusky fern
#

@misty kelp

misty kelp
#

nah

queen flame
vague kindle
misty kelp
#

he is saying the script aint readible

queen flame
dusky fern
dusky fern
#

my bad bro

#

im r3tadsardedad

#

should i copy msg tex there?

queen flame
#

Just edit your current one above the chat

dusky fern
#

function onHumanoidDied(humanoid, player)
local killer = getKillerOfHumanoidIfStillInGame(humanoid)
if (killer ~= nil) then
handleTKs(player, killer)
end
end

function onPlayerRespawn(property, player)
-- need to connect to new humanoid

if property == "Character" and player.Character ~= nil then
    local humanoid = player.Character.Humanoid
    local p = player
    local h = humanoid
    humanoid.Died:connect(function() onHumanoidDied(h, p) end )
end

end

function getKillerOfHumanoidIfStillInGame(humanoid)
-- returns the player object that killed this humanoid
-- returns nil if the killer is no longer in the game

-- check for kill tag on humanoid - may be more than one - todo: deal with this
local tag = humanoid:findFirstChild("creator")

-- find player with name on tag
if tag ~= nil then
    local killer = tag.Value
    if killer.Parent ~= nil then -- killer still in game
        return killer
    end
end

return nil

end

function handleTKs(player, killer)
if (killer.TeamColor == player.TeamColor) then
killer.Character:MoveTo( Vector3.new(-308, 3.4, 129) ) -- Insert Here The Position of the Noob House
wait(30)
killer.Character.Humanoid.Health = 0
wait(7)
killer.Character.Humanoid.Health = 0
end
end

function onPlayerEntered(newPlayer)
while true do
if newPlayer.Character ~= nil then break end
wait(3)
end

local humanoid = newPlayer.Character.Humanoid

humanoid.Died:connect(function() onHumanoidDied(humanoid, newPlayer) end )
newPlayer.Changed:connect(function(property) onPlayerRespawn(property, newPlayer) end )

end

game.Players.ChildAdded:connect(onPlayerEntered)

#

formated it hopefully its a bit better

vague kindle
dusky fern
#

is that beter

vague kindle
# dusky fern is that beter
function onHumanoidDied(humanoid, player)
    local killer = getKillerOfHumanoidIfStillInGame(humanoid)
    if (killer ~= nil) then
        handleTKs(player, killer)
    end
end

function onPlayerRespawn(property, player)
    -- need to connect to new humanoid

    if property == "Character" and player.Character ~= nil then
        local humanoid = player.Character.Humanoid
        local p = player
        local h = humanoid
        humanoid.Died:connect(function() onHumanoidDied(h, p) end )
    end
end

function getKillerOfHumanoidIfStillInGame(humanoid)
    -- returns the player object that killed this humanoid
    -- returns nil if the killer is no longer in the game

    -- check for kill tag on humanoid - may be more than one - todo: deal with this
    local tag = humanoid:findFirstChild("creator")

    -- find player with name on tag
    if tag ~= nil then
        local killer = tag.Value
        if killer.Parent ~= nil then -- killer still in game
            return killer
        end
    end

    return nil
end

function handleTKs(player, killer)
    if (killer.TeamColor == player.TeamColor) then
        killer.Character:MoveTo( Vector3.new(-308, 3.4, 129) ) -- Insert Here The Position of the Noob House
        wait(30)
        killer.Character.Humanoid.Health = 0
        wait(7)
        killer.Character.Humanoid.Health = 0
    end
end

function onPlayerEntered(newPlayer)
    while true do
        if newPlayer.Character ~= nil then break end
        wait(3)
    end

    local humanoid = newPlayer.Character.Humanoid

    humanoid.Died:connect(function() onHumanoidDied(humanoid, newPlayer) end )
    newPlayer.Changed:connect(function(property) onPlayerRespawn(property, newPlayer) end )
end

game.Players.ChildAdded:connect(onPlayerEntered)

This is better. You have to use three back ticks before the first line of the text and three back ticks after.

misty kelp
#

is there any way u can help

queen flame
#

@dusky fern my question is how code should work and WHAT exaclty doenst work

#

Killer isnt killed?

misty kelp
#

we want to fix these one of the scripter changed it

#

we have gun system we want like the we cant dmg our own teammates

vague kindle
#

A lot of what you're using isn't quite right.

You should be using PlayerAdded from Players, not ChildAdded

You don't have to use a while loop to wait for the character, just something like local char = newPlayer.Character or newPlayer.CharacterAdded:Wait(), which grabs the character, but if it's nil, it waits for the event

but every new character has a new humanoid, so you'd need to use CharacterAdded instead of PlayerAdded

.Changed will fire when any property whatsoever changes, so that's not a good choice of event.

I'm not sure why you're waiting 30 whole seconds before the killer dies, and then another 7 before they... die again? That's unsafe code as what if something happens within those 30 seconds?

dusky fern
#

Typos / outdated API

findFirstChild ➜ must be FindFirstChild (capital F, C).

:connect ➜ use :Connect.

Players.ChildAdded ➜ use Players.PlayerAdded.

Polling Character + Changed is brittle ➜ use player.CharacterAdded.

Team check

TeamColor is deprecated-ish. Compare teams with player.Team == killer.Team.

Nil-safety

Humanoids/Characters can be nil at times. Always WaitForChild("Humanoid") and re-check before using.

When you teleport/kill the TKer, verify killer.Character and its Humanoid still exist.

“creator” tag

Your logic assumes weapons set humanoid.creator (an ObjectValue) to the Player. Make sure your damage code does this; otherwise getKiller… will always return nil.

#

this what gpt said to us too

misty kelp
grim quailBOT
#

studio** You are now Level 1! **studio

dusky fern
# vague kindle A lot of what you're using isn't quite right. You should be using `PlayerAdded`...

-- Teamkill handler (ServerScriptService)

local Players = game:GetService("Players")

-- CONFIG: where to send TKers before punishment
local NOOB_HOUSE_POS = Vector3.new(-308, 3.4, 129)

local function getKillerIfStillInGame(humanoid: Humanoid): Player?
-- Standard convention: tools set an ObjectValue named "creator" on the Humanoid
local tag = humanoid:FindFirstChild("creator")
if tag and tag:IsA("ObjectValue") then
local killer = tag.Value
if typeof(killer) == "Instance" and killer:IsA("Player") and killer.Parent == Players then
return killer
end
end
return nil
end

local function handleTK(victim: Player, killer: Player)
-- Same team? (use Team, not TeamColor)
if killer.Team ~= nil and victim.Team ~= nil and killer.Team == victim.Team then
-- Try to move, then punish
local character = killer.Character
if character then
local hrp = character:FindFirstChild("HumanoidRootPart")
if hrp then
hrp.CFrame = CFrame.new(NOOB_HOUSE_POS)
else
character:MoveTo(NOOB_HOUSE_POS)
end
-- small delay to avoid race conditions
task.wait(0.1)
local hum = character:FindFirstChildOfClass("Humanoid")
if hum then
-- Example punishment: two instant deaths with a delay
hum.Health = 0
task.wait(7)
if character.Parent and hum.Parent then
hum.Health = 0
end
end
end
end
end

local function onHumanoidDied(humanoid: Humanoid, victimPlayer: Player)
local killer = getKillerIfStillInGame(humanoid)
if killer then
handleTK(victimPlayer, killer)
end
end

local function hookCharacter(player: Player, character: Model)
-- Wait for Humanoid safely
local humanoid = character:WaitForChild("Humanoid", 10)
if not humanoid then return end
humanoid.Died:Connect(function()
onHumanoidDied(humanoid, player)
end)
end

Players.PlayerAdded:Connect(function(player: Player)
-- Current character (if it already exists when the script starts)
if player.Character then
hookCharacter(player, player.Character)
end
-- Future spawns
player.CharacterAdded:Connect(function(char)
hookCharacter(player, char)
end)
end)

-- Also handle players already in-game if script is inserted while live:
for _, plr in ipairs(Players:GetPlayers()) do
if plr.Character then
hookCharacter(plr, plr.Character)
end
plr.CharacterAdded:Connect(function(char)
hookCharacter(plr, char)
end)
end
gpt gave me this code but doesnt work eithier lol

vague kindle
dusky fern
#

why?

misty kelp
#

local Players = game:GetService("Players")

-- CONFIG: where to send TKers before punishment
local NOOB_HOUSE_POS = Vector3.new(-308, 3.4, 129)

local function getKillerIfStillInGame(humanoid: Humanoid): Player?
    -- Standard convention: tools set an ObjectValue named "creator" on the Humanoid
    local tag = humanoid:FindFirstChild("creator")
    if tag and tag:IsA("ObjectValue") then
        local killer = tag.Value
        if typeof(killer) == "Instance" and killer:IsA("Player") and killer.Parent == Players then
            return killer
        end
    end
    return nil
end

local function handleTK(victim: Player, killer: Player)
    -- Same team? (use Team, not TeamColor)
    if killer.Team ~= nil and victim.Team ~= nil and killer.Team == victim.Team then
        -- Try to move, then punish
        local character = killer.Character
        if character then
            local hrp = character:FindFirstChild("HumanoidRootPart")
            if hrp then
                hrp.CFrame = CFrame.new(NOOB_HOUSE_POS)
            else
                character:MoveTo(NOOB_HOUSE_POS)
            end
            -- small delay to avoid race conditions
            task.wait(0.1)
            local hum = character:FindFirstChildOfClass("Humanoid")
            if hum then
                -- Example punishment: two instant deaths with a delay
                hum.Health = 0
                task.wait(7)
                if character.Parent and hum.Parent then
                    hum.Health = 0
                end
            end
        end
    end
end

local function onHumanoidDied(humanoid: Humanoid, victimPlayer: Player)
    local killer = getKillerIfStillInGame(humanoid)
    if killer then
        handleTK(victimPlayer, killer)
    end
end

local function hookCharacter(player: Player, character: Model)
    -- Wait for Humanoid safely
    local humanoid = character:WaitForChild("Humanoid", 10)
    if not humanoid then return end
    humanoid.Died:Connect(function()
        onHumanoidDied(humanoid, player)
    end)
end

Players.PlayerAdded:Connect(function(player: Player)
    -- Current character (if it already exists when the script starts)
    if player.Character then
        hookCharacter(player, player.Character)
    end
    -- Future spawns
    player.CharacterAdded:Connect(function(char)
        hookCharacter(player, char)
    end)
end)

-- Also handle players already in-game if script is inserted while live:
for _, plr in ipairs(Players:GetPlayers()) do
    if plr.Character then
        hookCharacter(plr, plr.Character)
    end
    plr.CharacterAdded:Connect(function(char)
        hookCharacter(plr, char)
    end)
end
gpt gave me this code but doesnt work eithier lol```
vague kindle
unborn prism
dusky fern
vague kindle
dusky fern
#

So no one can help?

unborn prism
#

No I'm sorry no one even helps me

dusky fern
#

hi