I am trying to make a script that checks if two players are touching the same part and if they are, it kills the one that touched the part first. This is my attempt at it, but it is really ugly. It runs without errors but it doesn't kill any players.

for _,item in pairs(workspace:GetDescendants()) do

		--lava parts
		if item.Name=="lava" then
			item.Touched:Connect(function(part)
				local hum=part.Parent:FindFirstChild("Humanoid")
				if hum then
					hum.Health=0
				end
			end)
		end
		
		--all other parts are paintable parts
		if item.Name=="paint" then
			item.Touched:Connect(function(part)
				local hum=part.Parent:FindFirstChild("Humanoid")
				if hum then
					local player=game.Players:WaitForChild(hum.Parent.Name)
					if player.TeamColor~=item.BrickColor then
						item.BrickColor=player.TeamColor
						item.Material=Enum.Material.Plastic
						local touchend=false
						item.Touched:Connect(function(otherpart)
							local hum=otherpart:FindFirstChild("Humanoid")
							if hum then
								local othername=game.Players:WaitForChild(hum.Parent.Name)
								if othername.Name~=player.Name and othername.TeamColor~=player.TeamColor then
									if not touchend then
										killPlayer(player)
									end
								end
							end
						end)
						item.TouchEnded:Connect(function(lastpart)
							local hum=lastpart:FindFirstChild("Humanoid")
							if hum then
								local lastname=game.Players:WaitForChild(hum.Parent.Name)
								if lastpart.Name==player.Name then
									touchend=true
								end
							end
						end)
					end
				end
			end)
		end
	end

2 comments

0

for line 8 try hm.Health = hm.Health-100 bc you cant just set a value like that too 0 without getting its current value