When a team player touch another teams player they die

I need some help with a script.
I have tried to look for it on internet, but cant find anything about it.
Im not a scripter. Im just a mom thats making a game with my kids :slight_smile:
I would like some help with this:
I need a script that makes one teams player touch the other teams player they die.
like this: team Police touch team Thief. Team Thief die. Nothing happens to team Police.
I have been thinking about using some kind of touch function. but since im not a scripter i cant really make it… thank you for helping me <3

3 Likes

A friend made this for me. but it doesnt work:
local Players = game:GetService(“Players”)

local function onCharacterAdded(character)
local player = Players:GetPlayerFromCharacter(character)
if player and player.Team then
local humanoid = character:WaitForChild(“Humanoid”)

    local function onTouch(other)
        local otherCharacter = other.Parent
        local otherPlayer = Players:GetPlayerFromCharacter(otherCharacter)

        if otherPlayer and otherPlayer.Team then
            -- Check if the player is on the Red team and the toucher is on the Blue team
            if player.Team.Name == "Red" and otherPlayer.Team.Name == "Blue" then
                -- Kill the Red team player
                humanoid.Health = 0
            end
        end
    end

    character.Touched:Connect(onTouch)
end

end

local function onPlayerAdded(player)
player.CharacterAdded:Connect(onCharacterAdded)
end

Players.PlayerAdded:Connect(onPlayerAdded)

i have also tried this that another friend made. doesnt work either
– Get the teams
local team1 = game.Teams.Team1
local team2 = game.Teams.Team2

– Function to kill player if touched by other team
local function onTouch(hit)
– Check if the player is on team1 and the hitter is on team2
if hit.Parent:FindFirstChild(“Humanoid”) and hit.Parent.Parent == team1 and game.Players:GetPlayerFromCharacter(hit.Parent) and game.Players:GetPlayerFromCharacter(script.Parent).Team == team2 then
– Kill the player
hit.Parent.Humanoid:TakeDamage(100)
end
end

– Connect the onTouch function to the Touched event of the player’s character
game.Players.PlayerAdded:Connect(function(player)
if player.Team == team1 then
player.CharacterAdded:Connect(function(character)
character:WaitForChild(“HumanoidRootPart”).Touched:Connect(onTouch)
end)
end
end)

maybe something that they use in hide and seek games?!

can i use something like this???
seeker.Character.HumanoidRootPart.Anchored = false

	for i, part in pairs(seeker.Character:GetChildren()) do
		if part:IsA("MeshPart") or part:IsA("BasePart") then 
			part.Touched:Connect(function(hit)
				if hit.Parent:FindFirstChild("Humanoid") then
					local player = Players:GetPlayerFromCharacter(hit.Parent)
					if player and not player:FindFirstChild("IsSeeker").Value then
						hit.Parent:SetPrimaryPartCFrame(workspace.SpawnLocation.CFrame)
						player.Character.HumanoidRootPart.Anchored = true
						for i, v in pairs(GamePlayers) do
							if v == player then
								table.remove(GamePlayers, i)
								break
							end
						end
					end
				end
			end)
		end
	end
1 Like

Hello! At a quick glance, I think the original code your friend gave you should work as long as your teams are ‘Red’ and ‘Blue’.

However, I did notice something. When I copied and pasted your code into my Roblox Studio, the “Players” and “Humanoid” in quotation marks were red underlined, so you might have used quotation symbols that don’t work. A bit of a strange solution, but maybe try using this code again, but replacing those words using copy and paste "Players" "Humanoid" and make sure to replace the old quotation marks with the new ones.

Let me know if it works?

1 Like

should i put it as a localscript in StarterPlayerScripts?

1 Like

It should be a server script (It’s grey and called ‘Script’) and put it in ServerScriptService.

1 Like

sorry it still doesnt work… not sure whats wrong

I have written some code that should help with your problem.

The first section of code below should be put into a localscript and placed into StarterCharacterScripts

local players = game:GetService("Players")
local deadEvent = game:GetService("ReplicatedStorage"):WaitForChild("DeadEvent")

local localplayer = players.LocalPlayer

local char = localplayer.Character or localplayer:WaitForChild("Character")

char.Humanoid.Touched:Connect(function(otherpart)
	
	if otherpart.Parent:FindFirstChild("Humanoid") and localplayer.Team.Name == "Police" then
		
		local otherplayer = players:GetPlayerFromCharacter(otherpart.Parent)
		
		if otherplayer.Team.Name == "Theif" then
			
			deadEvent:FireServer(otherplayer)
			
		end
		
	end
	
end)

You will also need to make a RemoteEvent named DeadEvent in order to handle the other players.

After that you will need to put the below code in script in ServerScriptStorage The code i’m placing below is the remote event that signals that the robber should die.

local deadEvent = game:GetService("ReplicatedStorage"):WaitForChild("DeadEvent")

deadEvent.OnServerEvent:Connect(function(player, otherplayer)
	
	local otherchar = otherplayer.Character or otherplayer:WaitForChild("Character")
	
	otherchar.Humanoid:TakeDamage(100)
	
	
end)

Try this and see if it works, if you have any problems let me know.

sorry but that doesnt work either…

That’s alright. If you go to the View tab and open up the Output box, then you can see if there’s any errors. If there are errors, you can click it and it’ll take you to where the problem is. Can you share if there’s anything in the output when you run the game?

no errors either… i really have no idea what to do

Are there any errors in the output tab?

i have to publish every time i want to try it out… haha

Can you run the game in studio? Go to the test tab and click ‘Play’. Then send us what the output looks like whilst it’s playing?

i cant. i have a error when i try that

1 Like

hmm

No need to, if you want to try your game out with more than one player to test it you can click on the Test tab at the top of studio and the Start Icon, making sure that you have selected more than 1 player.

how do i select more than 1 player?