Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Anti-Team Kill [When a player TKs then they get killed and deduction of Credits] Any Help?

I am looking for a script that kills a player and deducts their Credits once they have Team Killed another player. Looking to subtract 500 credits to that player. I have tried doing this but it hasn't worked.

Here is the script so far.

print("TK script loaded.")

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.Humanoid.Health = 0
	if stats ~= nil then
	local cash = stats:killer("Credits")
	cash.Value  = cash.Value -100
	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)

Where I am trying to add the deduction is near line [45 ]

1 answer

✓ Accepted answer

stats in line 44 is always nil, and therefore the credits are never reducted. Supposing that by stats you meant leaderstats, you should instead do what i did there:

print("TK script loaded.")

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") --findFirstChild is deprecated

    -- 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.Humanoid.Health = 0
    killer.leaderstats.Credits.Value  = killer.leaderstats.Credits.Value - 500 --leaderstats are included per player, you must refer to it inside killer.
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)

I also replaced a deprecated function. This should work now!

4 comments

0

Ill take a look at IT! Thank you so much for the help!

0

Remember to accept my answer. The "Accept Answer" button is above the "Report to Staff" button.

0

The only thing you forgot was adding an end at line 46, but other than that it worked sucessfully! Great Job.(I always like to check out the script before I accept the answer).

0

Yeah, i told you to accept my answer because there's many people that keep forgetting to.

This discussion is preserved as it was. New answers, comments, and votes are closed.