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

Team killing script not working, player supposed to die if they team kill?

I have 4 types of classes in my game Culprit, Officer, Agent, And Innocent.

What I want to happen is if the Officer or Agent kill each other or kill an Innocent they die.

I have the script I am using now but its not worth posting because it honestly so far off and it doesnt work XD

Theoretically you would check if the player is the officer or agent and then once thats done you would wait for someone to be killed and if the officer killed an innocent then his humanoid.Health would go to 0 but if he killed the culprit nothing would happen and same with the agent. I know how it would work in my mind I just dont know how id actually script it.

Hopefully someone can help me out :) Thanks for your time

Heres the script I have, I decided to add it

function newcharacter(bloxxedplayer, character)	
	local humanoid = character:FindFirstChild("Humanoid")
	if  humanoid then
		humanoid.Died:connect(function()
		end)
	end
end

-- Officer and agent no kill good guys
function newcharacter(bloxxedplayer, character)
	local humanoid = character:FindFirstChild("Humanoid")
	if humanoid then
		humanoid.Died:connect(function()
			
			local tag = humanoid:FindFirstChild("creator")
			if tag and tag.Value and tag.Value.Parent == game.Players then
				if tag.Value ~= culprit and agent and bloxxedplayer ~= culprit then
					local wrongdoer = tag.Value
					if wrongdoer.Character and wrongdoer.Character.Parent then
						local wrongdoerhumanoid = wrongdoer.Character:FindFirstChild("Humanoid")
						if wrongdoerhumanoid then
							wrongdoerhumanoid.Health = 0
						end
					end
				end
			end 
		end)
	end
end

There you go :)

I was told I should add the whole script so here it is at about line 50 is the script above.

--Made by Kofikingston9000


local roundtime = 60 * 4
local intermissiontime = 20


local datastore = game:GetService("DataStoreService"):GetDataStore("PlayerData")
local serverstorage = game:GetService("ServerStorage")
local replicatedstorage = game:GetService("ReplicatedStorage")
local event = replicatedstorage:WaitForChild("RemoteEvent")
local maps = serverstorage:WaitForChild("Maps")
local mapholder = game.Workspace:WaitForChild("MapHolder")
local statustag = replicatedstorage:WaitForChild("StatusTag")
local timertag = replicatedstorage:WaitForChild("TimerTag")

local playerdata = {}

--set up score leaderboard
	--keep track of points

function permanentsave(player)
	if player then
		local mydata = playerdata[player.userId]
		if mydata then
			datastore:SetAsync(player.userId, mydata)
		end
	end
end

game.Players.PlayerRemoving:connect(function(player)
	if player then
		permanentsave(player)
		playerdata[player.userId] = nil
	end
end)

game.OnClose = function()
	for i, v in pairs(playerdata) do
		datastore:SetAsync(i, v)
	end
end


function findplayerbyid(userid)
	for _, player in pairs(game.Players:GetPlayers()) do
		if player and player.userId == userid then
			return player
		end
	end
end

-- Officer and agent no kill good guys
function newcharacter(bloxxedplayer, character)
	local humanoid = character:FindFirstChild("Humanoid")
	if humanoid then
		humanoid.Died:connect(function()
			
			local tag = humanoid:FindFirstChild("creator")
			if tag and tag.Value and tag.Value.Parent == game.Players then
				if tag.Value ~= culprit and agent and bloxxedplayer ~= culprit then
					local wrongdoer = tag.Value
					if wrongdoer.Character and wrongdoer.Character.Parent then
						local wrongdoerhumanoid = wrongdoer.Character:FindFirstChild("Humanoid")
						if wrongdoerhumanoid then
							wrongdoerhumanoid.Health = 0
						end
					end
				end
			end 
		end)
	end
end

while true do
	--load a random map
	statustag.Value = "Loading Map"
	timertag.Value = -1
	mapholder:ClearAllChildren()
	wait(2)
	local allmaps = maps:GetChildren()
	local newmap = allmaps[math.random(1, #allmaps)]:clone()
	newmap.Parent = mapholder
	wait(2)
	
	--wait for contestants
	while true do
		wait(4)
		contestants = {}
		for _, player in pairs(game.Players:GetPlayers()) do
			if player and player.Character then
				local humanoid = player.Character:WaitForChild("Humanoid")
				if humanoid and humanoid.Health > 0 then
					table.insert(contestants, player)
				end
			end
		end
		if #contestants >= 3 then
			wait(5)
			break
		else
			statustag.Value = "Waiting for Players"
			timertag.Value = -1
		end
	end

	statustag.Value = "Match Time"
	timertag.Value = roundtime

	-- Choose culprit
	culprit = contestants[math.random(1, #contestants)]
	
	--choose sheriff
	while true do
		randomplayer = contestants[math.random(1, #contestants)]
		if randomplayer ~= culprit then
			officer = randomplayer
			break
		end
	end
	
	-- Choose the Agent
	while true do 
		randomplayer = contestants[math.random(1, #contestants)]
		if randomplayer ~= culprit and officer then
			agent = randomplayer
			break
		end
	end	
	--teleport contestants
	local spawnsmodel = newmap:WaitForChild("Spawns")
	local spawns = spawnsmodel:GetChildren()
	for _, player in pairs(contestants) do
		if player and player.Character and #spawns > 0 then
			local torso = player.Character:WaitForChild("Torso")
			local humanoid = player.Character:WaitForChild("Humanoid")
			local spawnindex = math.random(1, #spawns)
			local spawn = spawns[spawnindex]
			if spawn and torso and humanoid then
				humanoid.Health = 100
				humanoid.WalkSpeed = 16
				table.remove(spawns, spawnindex)
				torso.CFrame = CFrame.new(spawn.Position + Vector3.new(0, 3, 0))
				
				local matchtag = Instance.new("StringValue")
				matchtag.Name = "MatchTag"
				matchtag.Parent = player.Character

				local backpack = player:FindFirstChild("Backpack")
				if backpack then
					if player == culprit then
						humanoid.WalkSpeed = 18
						local machete = serverstorage:WaitForChild("Machete"):clone()
						machete.Parent = backpack
						local lantern = serverstorage:WaitForChild("Lantern"):clone()
						lantern.Parent = backpack
						event:FireClient(player, "Class", "Culprit")
						
					elseif player == officer then
						local machete = serverstorage:WaitForChild("Machete"):clone()
						machete.Parent = backpack
						event:FireClient(player, "Class", "Officer")
						
					elseif player == agent then
						local machete = serverstorage:WaitForChild("Machete"):clone()
						machete.Parent = backpack
						event:FireClient(player, "Class", "Agent")
							
					else
						local lantern = serverstorage:WaitForChild("Lantern"):clone()
						lantern.Parent = backpack
						event:FireClient(player, "Class", "Victim")
					end
				end
			end
		end
	end
	spawnsmodel:remove()

	--wait for round end conditions
	local localtimer = roundtime
	while localtimer > 0 do
		wait(1)
		localtimer = localtimer - 1
		timertag.Value = localtimer

		activecontestants = {}
		culpritactive = false
		for _, player in pairs(contestants) do
			if player then
				local character = player.Character
				if character then
					local matchtag = character:FindFirstChild("MatchTag")
					local humanoid = character:FindFirstChild("Humanoid")
					if matchtag and humanoid and humanoid.Health > 0 then
						if player == culprit then
							culpritactive = true
						end
						table.insert(activecontestants, player)
					end
				end
			end
		end
		if #activecontestants <= 1 or not culpritactive then
			break
		end
	end

	--sort game result
	local gameresult = "PlayersWin"
	if culpritactive then
		--innocent win
		if #activecontestants >= 2 then
			event:FireAllClients("Result", "PlayersWin")
		else
			--culprit win
			gameresult = "CulpritWin"
			event:FireAllClients("Result", "CulpritWin")
		end
	else
		--innocent win
		event:FireAllClients("Result", "PlayersWin")
	end
	for _, v in pairs(game.Workspace:GetChildren()) do
		if v then
			if v.Name == "Pickup" or v.className == "Hat" then
				v:remove()
			end
		end
	end
	
	--teleport plays back to lobby
	local lobbyspawns = {}
	for _, v in pairs(game.Workspace:WaitForChild("Lobby"):GetChildren()) do
		if v and v.Name == "SpawnLocation" then
			table.insert(lobbyspawns, v)
		end
	end
	for _, player in pairs(activecontestants) do
		if player then
			if player.Character then
				local humanoid = player.Character:FindFirstChild("Humanoid")
				if humanoid then
					humanoid:UnequipTools()
				end
			end

			local randomspawn = lobbyspawns[math.random(1, #lobbyspawns)]
			player.Character:MoveTo(randomspawn.Position)

			local backpack = player:FindFirstChild("Backpack")
			if backpack then
				backpack:ClearAllChildren()
			end
		end
	end
	culprit = nil

	--intermission wait
	statustag.Value = "Intermission"
	timertag.Value = intermissiontime
	local localtimer = intermissiontime
	while localtimer > 0 do
		wait(1)
		localtimer = localtimer - 1
		timertag.Value = localtimer
	end
end


1 answer

Yeah, i saw this same question earlier today, but I didn't respond because it didn't have time. Not sure why they removed it though. Try not to use the word S*** as it may be a catalyst for a moderation :p

Anways, wha it think was wrong is you did something like

--Tag was the creator tag
if tag.Value ~= Culprit -- Blah blah blah

The value of the creator tag is the name of the person who kiled him. You would have to do

tag.Value = player

if player.Team ~= game.Teams.Cuplrit --Blah blah blah

correct me if I misunderstood something

5 comments

0

Here ill edit it and add the full script maybe you can read that and see what I maybe doing wrong :) and ill edit the description XD

0

Okay i added the main stuff, I honestly dont understand why it doesnt work. Its literally the last bug in the entire game I just need to get it fixed ya know XD.

0

Ok there are two things that could go wrong, either the function you made is not being fired or one of the if statements is wrong. I cannont definitively tell because it seems you have left out alot of code. Give more more info. Also, are all the roles teams( roblox teams)? or are you doing something custom?

0

I added the whole script @SpyGuyTBM

0

ok, correct me if im wrong, but from what i see, you defined the function "new character", but you never used it in you script, i.e called it

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